xoslib wip
[plstackapi.git] / planetstack / core / xoslib / static / js / xos-backbone.js
1 SLIVER_API = "/plstackapi/slivers/";
2 SLICE_API = "/plstackapi/slices/";
3
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
28 XOSCollection = Backbone.Collection.extend({
29     objects: function() {
30                 return this.models.map(function(element) { return element.attributes; });
31              },
32
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
64         model = new this.model({
65             resource_uri: id
66         });
67
68         model.fetch(options);
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         },
90 });
91
92 function xoslib() {
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();
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();
102 };
103
104 xos = new xoslib();