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