blob: c4f1a33821e34c1a44826ff895a142518b61e760 [file] [log] [blame]
Scott Baker1dd345e2014-07-07 17:06:07 -07001SLIVER_API = "/plstackapi/slivers/";
Scott Baker95d6c5c2014-07-07 21:54:35 -07002SLICE_API = "/plstackapi/slices/";
Scott Baker1dd345e2014-07-07 17:06:07 -07003
Scott Bakerb7aba682014-07-07 21:30:52 -07004XOSModel = 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 Baker1dd345e2014-07-07 17:06:07 -070028XOSCollection = Backbone.Collection.extend({
Scott Bakerb7aba682014-07-07 21:30:52 -070029 objects: function() {
30 return this.models.map(function(element) { return element.attributes; });
31 },
32
Scott Baker1dd345e2014-07-07 17:06:07 -070033 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 Bakerb7aba682014-07-07 21:30:52 -070064 model = new this.model({
Scott Baker1dd345e2014-07-07 17:06:07 -070065 resource_uri: id
66 });
67
68 model.fetch(options);
Scott Bakerb7aba682014-07-07 21:30:52 -070069 },
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 Baker1dd345e2014-07-07 17:06:07 -070090});
91
92function xoslib() {
Scott Bakerb7aba682014-07-07 21:30:52 -070093 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 Baker95d6c5c2014-07-07 21:54:35 -070097
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 Baker1dd345e2014-07-07 17:06:07 -0700102};
103
Scott Baker95d6c5c2014-07-07 21:54:35 -0700104xos = new xoslib();