refactor code organization
[plstackapi.git] / planetstack / core / xoslib / static / js / xoslib / 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                 var url = this.attributes.resource_uri;
15
16                 if (!url) {
17                     url = this.urlRoot + this.id;
18                 }
19
20                 if (!url) {
21                     // XXX I'm not sure this does anything useful
22                     url = ( _.isFunction( this.collection.url ) ? this.collection.url() : this.collection.url );
23                     url = url || this.urlRoot;
24                 }
25
26                 // remove any existing query parameters
27                 url && ( url.indexOf("?") > -1 ) && ( url = url.split("?")[0] );
28
29                 url && ( url += ( url.length > 0 && url.charAt( url.length - 1 ) === '/' ) ? '' : '/' );
30
31                 url && ( url += "?no_hyperlinks=1" );
32
33                 return url;
34         },
35
36         listMethods: function() {
37             var res = [];\r
38             for(var m in this) {\r
39                 if(typeof this[m] == "function") {\r
40                     res.push(m)\r
41                 }\r
42             }\r
43             return res;\r
44         }
45 });
46
47 XOSCollection = Backbone.Collection.extend({
48     objects: function() {
49                 return this.models.map(function(element) { return element.attributes; });
50              },
51
52     maybeFetch: function(options){
53             // Helper function to fetch only if this collection has not been fetched before.
54         if(this._fetched){
55                 // If this has already been fetched, call the success, if it exists
56             options.success && options.success();
57             console.log("alreadyFetched");
58             return;
59         }
60
61             // when the original success function completes mark this collection as fetched
62         var self = this,
63         successWrapper = function(success){
64             return function(){
65                 self._fetched = true;
66                 success && success.apply(this, arguments);
67             };
68         };
69         options.success = successWrapper(options.success);
70         console.log("call fetch");
71         this.fetch(options);
72     },
73
74     getOrFetch: function(id, options){
75             // Helper function to use this collection as a cache for models on the server
76         var model = this.get(id);
77
78         if(model){
79             options.success && options.success(model);
80             return;
81         }
82
83         model = new this.model({
84             resource_uri: id
85         });
86
87         model.fetch(options);
88     },
89
90     /* from backbone-tastypie.js */
91     url: function( models ) {
92                 var url = this.urlRoot || ( models && models.length && models[0].urlRoot );
93                 url && ( url += ( url.length > 0 && url.charAt( url.length - 1 ) === '/' ) ? '' : '/' );
94
95                 // Build a url to retrieve a set of models. This assume the last part of each model's idAttribute
96                 // (set to 'resource_uri') contains the model's id.
97                 if ( models && models.length ) {
98                         var ids = _.map( models, function( model ) {
99                                         var parts = _.compact( model.id.split('/') );
100                                         return parts[ parts.length - 1 ];
101                                 });
102                         url += 'set/' + ids.join(';') + '/';
103                 }
104
105                 url && ( url += "?no_hyperlinks=1" );
106
107                 return url;
108         },
109
110         listMethods: function() {
111             var res = [];\r
112             for(var m in this) {\r
113                 if(typeof this[m] == "function") {\r
114                     res.push(m)\r
115                 }\r
116             }\r
117             return res;\r
118         }
119 });
120
121 function xoslib() {
122     this.sliver = XOSModel.extend({ urlRoot: SLIVER_API });
123     this.sliverCollection = XOSCollection.extend({ urlRoot: SLIVER_API,
124                                                    model: this.sliver});
125     this.slivers = new this.sliverCollection();
126
127     this.slice = XOSModel.extend({ urlRoot: SLICE_API });
128     this.sliceCollection = XOSCollection.extend({ urlRoot: SLICE_API,
129                                                    model: this.slice});
130     this.slices = new this.sliceCollection();
131
132     this.node = XOSModel.extend({ urlRoot: NODE_API });
133     this.nodeCollection = XOSCollection.extend({ urlRoot: NODE_API,
134                                                    model: this.node});
135     this.nodes = new this.nodeCollection();
136
137     this.site = XOSModel.extend({ urlRoot: SITE_API });
138     this.siteCollection = XOSCollection.extend({ urlRoot: SITE_API,
139                                                    model: this.site});
140     this.sites = new this.siteCollection();
141
142     this.user = XOSModel.extend({ urlRoot: USER_API });
143     this.userCollection = XOSCollection.extend({ urlRoot: USER_API,
144                                                    model: this.user});
145     this.users = new this.userCollection();
146
147     this.deployment = XOSModel.extend({ urlRoot: DEPLOYMENT_API });
148     this.deploymentCollection = XOSCollection.extend({ urlRoot: DEPLOYMENT_API,
149                                                        model: this.deployment});
150     this.deployments = new this.deploymentCollection();
151
152     this.listObjects = function() { return ["slivers", "slices", "nodes", "sites", "users", "deployments"]; };
153 };
154
155 xos = new xoslib();
156