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