Scott Baker | 1dd345e | 2014-07-07 17:06:07 -0700 | [diff] [blame] | 1 | SLIVER_API = "/plstackapi/slivers/"; |
Scott Baker | 95d6c5c | 2014-07-07 21:54:35 -0700 | [diff] [blame] | 2 | SLICE_API = "/plstackapi/slices/"; |
Scott Baker | 1dd345e | 2014-07-07 17:06:07 -0700 | [diff] [blame] | 3 | |
Scott Baker | b7aba68 | 2014-07-07 21:30:52 -0700 | [diff] [blame] | 4 | XOSModel = Backbone.Model.extend({ |
| 5 | /* from backbone-tastypie.js */ |
| 6 | idAttribute: 'resource_uri', |
| 7 | |
| 8 | /* from backbone-tastypie.js */ |
| 9 | url: function() { |
| 10 | // Use the id if possible |
| 11 | var url = this.id; |
| 12 | |
| 13 | // If there's no idAttribute, try to have the collection construct a url. Fallback to 'urlRoot'. |
| 14 | if ( !url ) { |
| 15 | url = this.collection && ( _.isFunction( this.collection.url ) ? this.collection.url() : this.collection.url ); |
| 16 | console.log(url); |
| 17 | url = url || this.urlRoot; |
| 18 | } |
| 19 | |
| 20 | url && ( url += ( url.length > 0 && url.charAt( url.length - 1 ) === '/' ) ? '' : '/' ); |
| 21 | |
| 22 | url && ( url += "?no_hyperlinks=1" ); |
| 23 | |
| 24 | return url; |
| 25 | }, |
| 26 | }); |
| 27 | |
Scott Baker | 1dd345e | 2014-07-07 17:06:07 -0700 | [diff] [blame] | 28 | XOSCollection = Backbone.Collection.extend({ |
Scott Baker | b7aba68 | 2014-07-07 21:30:52 -0700 | [diff] [blame] | 29 | objects: function() { |
| 30 | return this.models.map(function(element) { return element.attributes; }); |
| 31 | }, |
| 32 | |
Scott Baker | 1dd345e | 2014-07-07 17:06:07 -0700 | [diff] [blame] | 33 | maybeFetch: function(options){ |
| 34 | // Helper function to fetch only if this collection has not been fetched before. |
| 35 | if(this._fetched){ |
| 36 | // If this has already been fetched, call the success, if it exists |
| 37 | options.success && options.success(); |
| 38 | console.log("alreadyFetched"); |
| 39 | return; |
| 40 | } |
| 41 | |
| 42 | // when the original success function completes mark this collection as fetched |
| 43 | var self = this, |
| 44 | successWrapper = function(success){ |
| 45 | return function(){ |
| 46 | self._fetched = true; |
| 47 | success && success.apply(this, arguments); |
| 48 | }; |
| 49 | }; |
| 50 | options.success = successWrapper(options.success); |
| 51 | console.log("call fetch"); |
| 52 | this.fetch(options); |
| 53 | }, |
| 54 | |
| 55 | getOrFetch: function(id, options){ |
| 56 | // Helper function to use this collection as a cache for models on the server |
| 57 | var model = this.get(id); |
| 58 | |
| 59 | if(model){ |
| 60 | options.success && options.success(model); |
| 61 | return; |
| 62 | } |
| 63 | |
Scott Baker | b7aba68 | 2014-07-07 21:30:52 -0700 | [diff] [blame] | 64 | model = new this.model({ |
Scott Baker | 1dd345e | 2014-07-07 17:06:07 -0700 | [diff] [blame] | 65 | resource_uri: id |
| 66 | }); |
| 67 | |
| 68 | model.fetch(options); |
Scott Baker | b7aba68 | 2014-07-07 21:30:52 -0700 | [diff] [blame] | 69 | }, |
| 70 | |
| 71 | /* from backbone-tastypie.js */ |
| 72 | url: function( models ) { |
| 73 | var url = this.urlRoot || ( models && models.length && models[0].urlRoot ); |
| 74 | url && ( url += ( url.length > 0 && url.charAt( url.length - 1 ) === '/' ) ? '' : '/' ); |
| 75 | |
| 76 | // Build a url to retrieve a set of models. This assume the last part of each model's idAttribute |
| 77 | // (set to 'resource_uri') contains the model's id. |
| 78 | if ( models && models.length ) { |
| 79 | var ids = _.map( models, function( model ) { |
| 80 | var parts = _.compact( model.id.split('/') ); |
| 81 | return parts[ parts.length - 1 ]; |
| 82 | }); |
| 83 | url += 'set/' + ids.join(';') + '/'; |
| 84 | } |
| 85 | |
| 86 | url && ( url += "?no_hyperlinks=1" ); |
| 87 | |
| 88 | return url; |
| 89 | }, |
Scott Baker | 1dd345e | 2014-07-07 17:06:07 -0700 | [diff] [blame] | 90 | }); |
| 91 | |
| 92 | function xoslib() { |
Scott Baker | b7aba68 | 2014-07-07 21:30:52 -0700 | [diff] [blame] | 93 | this.sliver = XOSModel.extend({ urlRoot: SLIVER_API }); |
| 94 | this.sliverCollection = XOSCollection.extend({ urlRoot: SLIVER_API, |
| 95 | model: this.sliver}); |
| 96 | this.slivers = new this.sliverCollection(); |
Scott Baker | 95d6c5c | 2014-07-07 21:54:35 -0700 | [diff] [blame] | 97 | |
| 98 | this.slice = XOSModel.extend({ urlRoot: SLICE_API }); |
| 99 | this.sliceCollection = XOSCollection.extend({ urlRoot: SLICE_API, |
| 100 | model: this.slice}); |
| 101 | this.slices = new this.sliceCollection(); |
Scott Baker | 1dd345e | 2014-07-07 17:06:07 -0700 | [diff] [blame] | 102 | }; |
| 103 | |
Scott Baker | 95d6c5c | 2014-07-07 21:54:35 -0700 | [diff] [blame] | 104 | xos = new xoslib(); |