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