From b7aba68631793a6960b8cb5338c42b3ffbe1baf4 Mon Sep 17 00:00:00 2001 From: Scott Baker Date: Mon, 7 Jul 2014 21:30:52 -0700 Subject: [PATCH] xoslib wip --- .../xoslib/dashboards/sliverListTest.html | 2 +- .../xoslib/static/js/backbone-tastypie.js | 95 +++++++++++++++++++ .../core/xoslib/static/js/sliverListTest.js | 39 +------- .../core/xoslib/static/js/xos-backbone.js | 59 +++++++++++- 4 files changed, 154 insertions(+), 41 deletions(-) create mode 100644 planetstack/core/xoslib/static/js/backbone-tastypie.js diff --git a/planetstack/core/xoslib/dashboards/sliverListTest.html b/planetstack/core/xoslib/dashboards/sliverListTest.html index 2238181..7a5b2d4 100644 --- a/planetstack/core/xoslib/dashboards/sliverListTest.html +++ b/planetstack/core/xoslib/dashboards/sliverListTest.html @@ -3,7 +3,7 @@ - + diff --git a/planetstack/core/xoslib/static/js/backbone-tastypie.js b/planetstack/core/xoslib/static/js/backbone-tastypie.js new file mode 100644 index 0000000..69a4d56 --- /dev/null +++ b/planetstack/core/xoslib/static/js/backbone-tastypie.js @@ -0,0 +1,95 @@ +/** + * Backbone-tastypie.js 0.1 + * (c) 2011 Paul Uithol + * + * Backbone-tastypie may be freely distributed under the MIT license. + * Add or override Backbone.js functionality, for compatibility with django-tastypie. + */ +(function( undefined ) { + var Backbone = this.Backbone; + + /** + * Override Backbone's sync function, to do a GET upon receiving a HTTP CREATED. + * This requires 2 requests to do a create, so you may want to use some other method in production. + * Modified from http://joshbohde.com/blog/backbonejs-and-django + */ + Backbone.oldSync = Backbone.sync; + Backbone.sync = function( method, model, options ) { + if ( method === 'create' ) { + var dfd = new $.Deferred(); + + // Set up 'success' handling + dfd.done( options.success ); + options.success = function( resp, status, xhr ) { + // If create is successful but doesn't return a response, fire an extra GET. + // Otherwise, resolve the deferred (which triggers the original 'success' callbacks). + if ( xhr.status === 201 && !resp ) { // 201 CREATED; response null or empty. + var location = xhr.getResponseHeader( 'Location' ); + return $.ajax( { + url: location, + success: dfd.resolve, + error: dfd.reject + }); + } + else { + return dfd.resolveWith( options.context || options, [ resp, status, xhr ] ); + } + }; + + // Set up 'error' handling + dfd.fail( options.error ); + options.error = dfd.reject; + + // Make the request, make it accessibly by assigning it to the 'request' property on the deferred + dfd.request = Backbone.oldSync( method, model, options ); + return dfd; + } + + return Backbone.oldSync( method, model, options ); + }; + + Backbone.Model.prototype.idAttribute = 'resource_uri'; + + Backbone.Model.prototype.url = function() { + // Use the id if possible + var url = this.id; + + // If there's no idAttribute, try to have the collection construct a url. Fallback to 'urlRoot'. + if ( !url ) { + url = this.collection && ( _.isFunction( this.collection.url ) ? this.collection.url() : this.collection.url ); + url = url || this.urlRoot; + } + + url && ( url += ( url.length > 0 && url.charAt( url.length - 1 ) === '/' ) ? '' : '/' ); + + return url; + }; + + /** + * Return 'data.objects' if it exists and is an array, or else just plain 'data'. + */ + Backbone.Model.prototype.parse = function( data ) { + return data && data.objects && ( _.isArray( data.objects ) ? data.objects[ 0 ] : data.objects ) || data; + }; + + Backbone.Collection.prototype.parse = function( data ) { + return data && data.objects; + }; + + Backbone.Collection.prototype.url = function( models ) { + var url = this.urlRoot || ( models && models.length && models[0].urlRoot ); + url && ( url += ( url.length > 0 && url.charAt( url.length - 1 ) === '/' ) ? '' : '/' ); + + // Build a url to retrieve a set of models. This assume the last part of each model's idAttribute + // (set to 'resource_uri') contains the model's id. + if ( models && models.length ) { + var ids = _.map( models, function( model ) { + var parts = _.compact( model.id.split('/') ); + return parts[ parts.length - 1 ]; + }); + url += 'set/' + ids.join(';') + '/'; + } + + return url; + }; +})(); diff --git a/planetstack/core/xoslib/static/js/sliverListTest.js b/planetstack/core/xoslib/static/js/sliverListTest.js index 294ffd3..2365aa2 100644 --- a/planetstack/core/xoslib/static/js/sliverListTest.js +++ b/planetstack/core/xoslib/static/js/sliverListTest.js @@ -2,7 +2,7 @@ window.SliverView = Backbone.View.extend({ tagName: 'li', - className: 'XOSLib.sliver', + className: 'sliver', events: { 'click .permalink': 'navigate' @@ -40,32 +40,6 @@ window.DetailApp = Backbone.View.extend({ } }); -window.InputView = Backbone.View.extend({ - events: { - 'click .sliver': 'createSliver', - 'keypress #message': 'createOnEnter' - }, - - createOnEnter: function(e){ - if((e.keyCode || e.which) == 13){ - this.createSliver(); - e.preventDefault(); - } - - }, - - createSliver: function(){ - var message = this.$('#message').val(); - if(message){ - this.collection.create({ - message: message - }); - this.$('#message').val(''); - } - } - -}); - window.ListView = Backbone.View.extend({ initialize: function(){ _.bindAll(this, 'addOne', 'addAll'); @@ -82,7 +56,7 @@ window.ListView = Backbone.View.extend({ addOne: function(sliver){ var view = new SliverView({ - model: XOSLib.sliver + model: sliver }); $(this.el).prepend(view.render().el); this.views.push(view); @@ -104,6 +78,7 @@ window.ListApp = Backbone.View.extend({ render: function(){ console.log("listApp.render"); + console.log(this.collection); $(this.el).html(ich.listApp({})); var list = new ListView({ collection: this.collection, @@ -111,10 +86,6 @@ window.ListApp = Backbone.View.extend({ }); list.addAll(); list.bind('all', this.rethrow, this); - new InputView({ - collection: this.collection, - el: this.$('#input') - }); } }); @@ -139,7 +110,7 @@ window.Router = Backbone.Router.extend({ $(function(){ window.app = window.app || {}; app.router = new Router(); - app.slivers = new XOSLib.slivers(); + app.slivers = XOSLib.slivers; //new XOSLib.slivers(); app.list = new ListApp({ el: $("#app"), collection: app.slivers @@ -148,13 +119,11 @@ $(function(){ el: $("#app") }); app.router.bind('route:list', function(){ - console.log("Router:list2"); app.slivers.maybeFetch({ success: _.bind(app.list.render, app.list) }); }); app.router.bind('route:detail', function(id){ - console.log("Router:detail2"); app.slivers.getOrFetch(app.slivers.urlRoot + id + '/', { success: function(model){ app.detail.model = model; diff --git a/planetstack/core/xoslib/static/js/xos-backbone.js b/planetstack/core/xoslib/static/js/xos-backbone.js index 99f0784..1633768 100644 --- a/planetstack/core/xoslib/static/js/xos-backbone.js +++ b/planetstack/core/xoslib/static/js/xos-backbone.js @@ -1,6 +1,34 @@ SLIVER_API = "/plstackapi/slivers/"; +XOSModel = Backbone.Model.extend({ + /* from backbone-tastypie.js */ + idAttribute: 'resource_uri', + + /* from backbone-tastypie.js */ + url: function() { + // Use the id if possible + var url = this.id; + + // If there's no idAttribute, try to have the collection construct a url. Fallback to 'urlRoot'. + if ( !url ) { + url = this.collection && ( _.isFunction( this.collection.url ) ? this.collection.url() : this.collection.url ); + console.log(url); + url = url || this.urlRoot; + } + + url && ( url += ( url.length > 0 && url.charAt( url.length - 1 ) === '/' ) ? '' : '/' ); + + url && ( url += "?no_hyperlinks=1" ); + + return url; + }, +}); + XOSCollection = Backbone.Collection.extend({ + objects: function() { + return this.models.map(function(element) { return element.attributes; }); + }, + maybeFetch: function(options){ // Helper function to fetch only if this collection has not been fetched before. if(this._fetched){ @@ -32,18 +60,39 @@ XOSCollection = Backbone.Collection.extend({ return; } - model = new Sliver({ + model = new this.model({ resource_uri: id }); model.fetch(options); - } + }, + + /* from backbone-tastypie.js */ + url: function( models ) { + var url = this.urlRoot || ( models && models.length && models[0].urlRoot ); + url && ( url += ( url.length > 0 && url.charAt( url.length - 1 ) === '/' ) ? '' : '/' ); + + // Build a url to retrieve a set of models. This assume the last part of each model's idAttribute + // (set to 'resource_uri') contains the model's id. + if ( models && models.length ) { + var ids = _.map( models, function( model ) { + var parts = _.compact( model.id.split('/') ); + return parts[ parts.length - 1 ]; + }); + url += 'set/' + ids.join(';') + '/'; + } + + url && ( url += "?no_hyperlinks=1" ); + + return url; + }, }); function xoslib() { - this.sliver = Backbone.Model.extend({ urlRoot: SLIVER_API }); - this.slivers = XOSCollection.extend({ urlRoot: SLIVER_API, - model: this.sliver}); + this.sliver = XOSModel.extend({ urlRoot: SLIVER_API }); + this.sliverCollection = XOSCollection.extend({ urlRoot: SLIVER_API, + model: this.sliver}); + this.slivers = new this.sliverCollection(); }; XOSLib = new xoslib(); -- 2.43.0