1 if (! window.XOSLIB_LOADED ) {
2 window.XOSLIB_LOADED=true;
4 SLIVER_API = "/plstackapi/slivers/";
5 SLICE_API = "/plstackapi/slices/";
6 NODE_API = "/plstackapi/nodes/";
7 SITE_API = "/plstackapi/sites/";
8 USER_API = "/plstackapi/users/";
9 DEPLOYMENT_API = "/plstackapi/deployments";
10 IMAGE_API = "/plstackapi/images";
11 NETWORK_API = "/plstackapi/networks";
12 SERVICE_API = "/plstackapi/services";
14 SLICEPLUS_API = "/xoslib/slicesplus/";
16 XOSModel = Backbone.Model.extend({
17 /* from backbone-tastypie.js */
18 //idAttribute: 'resource_uri',
20 /* from backbone-tastypie.js */
22 var url = this.attributes.resource_uri;
25 url = this.urlRoot + this.id;
29 // XXX I'm not sure this does anything useful
30 url = ( _.isFunction( this.collection.url ) ? this.collection.url() : this.collection.url );
31 url = url || this.urlRoot;
34 // remove any existing query parameters
35 url && ( url.indexOf("?") > -1 ) && ( url = url.split("?")[0] );
37 url && ( url += ( url.length > 0 && url.charAt( url.length - 1 ) === '/' ) ? '' : '/' );
39 url && ( url += "?no_hyperlinks=1" );
44 listMethods: function() {
46 for(var m in this) {
\r
47 if(typeof this[m] == "function") {
\r
55 XOSCollection = Backbone.Collection.extend({
57 return this.models.map(function(element) { return element.attributes; });
60 initialize: function(){
61 this.sortVar = 'name';
\r
62 this.sortOrder = 'asc';
\r
65 simpleComparator: function( model ){
\r
66 parts=this.sortVar.split(".");
\r
67 result = model.get(parts[0]);
\r
68 for (index=1; index<parts.length; ++index) {
\r
69 result=result[parts[index]];
\r
74 comparator: function (left, right) {
\r
75 var l = this.simpleComparator(left);
\r
76 var r = this.simpleComparator(right);
\r
78 if (l === void 0) return -1;
\r
79 if (r === void 0) return 1;
\r
81 if (this.sortOrder=="desc") {
\r
82 return l < r ? 1 : l > r ? -1 : 0;
\r
84 return l < r ? -1 : l > r ? 1 : 0;
\r
88 startPolling: function() {
\r
91 setInterval(function() { collection.fetch(); }, 10000);
97 maybeFetch: function(options){
98 // Helper function to fetch only if this collection has not been fetched before.
100 // If this has already been fetched, call the success, if it exists
101 options.success && options.success();
102 console.log("alreadyFetched");
106 // when the original success function completes mark this collection as fetched
108 successWrapper = function(success){
110 self._fetched = true;
111 success && success.apply(this, arguments);
114 options.success = successWrapper(options.success);
115 console.log("call fetch");
119 getOrFetch: function(id, options){
120 // Helper function to use this collection as a cache for models on the server
121 var model = this.get(id);
124 options.success && options.success(model);
128 model = new this.model({
132 model.fetch(options);
135 /* from backbone-tastypie.js */
136 url: function( models ) {
137 var url = this.urlRoot || ( models && models.length && models[0].urlRoot );
138 url && ( url += ( url.length > 0 && url.charAt( url.length - 1 ) === '/' ) ? '' : '/' );
140 // Build a url to retrieve a set of models. This assume the last part of each model's idAttribute
141 // (set to 'resource_uri') contains the model's id.
142 if ( models && models.length ) {
143 var ids = _.map( models, function( model ) {
144 var parts = _.compact( model.id.split('/') );
145 return parts[ parts.length - 1 ];
147 url += 'set/' + ids.join(';') + '/';
150 url && ( url += "?no_hyperlinks=1" );
155 listMethods: function() {
157 for(var m in this) {
\r
158 if(typeof this[m] == "function") {
\r
168 this.sliver = XOSModel.extend({ urlRoot: SLIVER_API });
169 this.sliverCollection = XOSCollection.extend({ urlRoot: SLIVER_API,
170 model: this.sliver});
171 this.slivers = new this.sliverCollection();
173 this.slice = XOSModel.extend({ urlRoot: SLICE_API });
174 this.sliceCollection = XOSCollection.extend({ urlRoot: SLICE_API,
176 this.slices = new this.sliceCollection();
178 this.node = XOSModel.extend({ urlRoot: NODE_API });
179 this.nodeCollection = XOSCollection.extend({ urlRoot: NODE_API,
181 this.nodes = new this.nodeCollection();
183 this.site = XOSModel.extend({ urlRoot: SITE_API });
184 this.siteCollection = XOSCollection.extend({ urlRoot: SITE_API,
186 this.sites = new this.siteCollection();
188 this.user = XOSModel.extend({ urlRoot: USER_API });
189 this.userCollection = XOSCollection.extend({ urlRoot: USER_API,
191 this.users = new this.userCollection();
193 this.deployment = XOSModel.extend({ urlRoot: DEPLOYMENT_API });
194 this.deploymentCollection = XOSCollection.extend({ urlRoot: DEPLOYMENT_API,
195 model: this.deployment});
196 this.deployments = new this.deploymentCollection();
198 this.image = XOSModel.extend({ urlRoot: IMAGE_API });
199 this.imageCollection = XOSCollection.extend({ urlRoot: IMAGE_API,
201 this.images = new this.imageCollection();
203 this.network = XOSModel.extend({ urlRoot: NETWORK_API });
204 this.networkCollection = XOSCollection.extend({ urlRoot: NETWORK_API,
205 model: this.network});
206 this.networks = new this.networkCollection();
208 this.service = XOSModel.extend({ urlRoot: SERVICE_API });
209 this.serviceCollection = XOSCollection.extend({ urlRoot: SERVICE_API,
210 model: this.service});
211 this.services = new this.serviceCollection();
214 this.slicePlus = XOSModel.extend({ urlRoot: SLICEPLUS_API });
215 this.slicePlusCollection = XOSCollection.extend({ urlRoot: SLICEPLUS_API,
216 model: this.slicePlus});
217 this.slicesPlus = new this.slicePlusCollection();
219 this.listObjects = function() { return ["slivers", "slices", "nodes", "sites", "users", "deployments"]; };