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";
13 SLICEPLUS_API = "/xoslib/slicesplus/";
15 XOSModel = Backbone.Model.extend({
16 /* from backbone-tastypie.js */
17 //idAttribute: 'resource_uri',
19 /* from backbone-tastypie.js */
21 var url = this.attributes.resource_uri;
24 url = this.urlRoot + this.id;
28 // XXX I'm not sure this does anything useful
29 url = ( _.isFunction( this.collection.url ) ? this.collection.url() : this.collection.url );
30 url = url || this.urlRoot;
33 // remove any existing query parameters
34 url && ( url.indexOf("?") > -1 ) && ( url = url.split("?")[0] );
36 url && ( url += ( url.length > 0 && url.charAt( url.length - 1 ) === '/' ) ? '' : '/' );
38 url && ( url += "?no_hyperlinks=1" );
43 listMethods: function() {
45 for(var m in this) {
\r
46 if(typeof this[m] == "function") {
\r
54 XOSCollection = Backbone.Collection.extend({
56 return this.models.map(function(element) { return element.attributes; });
59 initialize: function(){
60 this.sortVar = 'name';
\r
61 this.sortOrder = 'asc';
\r
64 simpleComparator: function( model ){
\r
65 parts=this.sortVar.split(".");
\r
66 result = model.get(parts[0]);
\r
67 for (index=1; index<parts.length; ++index) {
\r
68 result=result[parts[index]];
\r
73 comparator: function (left, right) {
\r
74 var l = this.simpleComparator(left);
\r
75 var r = this.simpleComparator(right);
\r
77 if (l === void 0) return -1;
\r
78 if (r === void 0) return 1;
\r
80 if (this.sortOrder=="desc") {
\r
81 return l < r ? 1 : l > r ? -1 : 0;
\r
83 return l < r ? -1 : l > r ? 1 : 0;
\r
87 startPolling: function() {
\r
90 setInterval(function() { collection.fetch(); }, 10000);
96 maybeFetch: function(options){
97 // Helper function to fetch only if this collection has not been fetched before.
99 // If this has already been fetched, call the success, if it exists
100 options.success && options.success();
101 console.log("alreadyFetched");
105 // when the original success function completes mark this collection as fetched
107 successWrapper = function(success){
109 self._fetched = true;
110 success && success.apply(this, arguments);
113 options.success = successWrapper(options.success);
114 console.log("call fetch");
118 getOrFetch: function(id, options){
119 // Helper function to use this collection as a cache for models on the server
120 var model = this.get(id);
123 options.success && options.success(model);
127 model = new this.model({
131 model.fetch(options);
134 /* from backbone-tastypie.js */
135 url: function( models ) {
136 var url = this.urlRoot || ( models && models.length && models[0].urlRoot );
137 url && ( url += ( url.length > 0 && url.charAt( url.length - 1 ) === '/' ) ? '' : '/' );
139 // Build a url to retrieve a set of models. This assume the last part of each model's idAttribute
140 // (set to 'resource_uri') contains the model's id.
141 if ( models && models.length ) {
142 var ids = _.map( models, function( model ) {
143 var parts = _.compact( model.id.split('/') );
144 return parts[ parts.length - 1 ];
146 url += 'set/' + ids.join(';') + '/';
149 url && ( url += "?no_hyperlinks=1" );
154 listMethods: function() {
156 for(var m in this) {
\r
157 if(typeof this[m] == "function") {
\r
167 this.sliver = XOSModel.extend({ urlRoot: SLIVER_API });
168 this.sliverCollection = XOSCollection.extend({ urlRoot: SLIVER_API,
169 model: this.sliver});
170 this.slivers = new this.sliverCollection();
172 this.slice = XOSModel.extend({ urlRoot: SLICE_API });
173 this.sliceCollection = XOSCollection.extend({ urlRoot: SLICE_API,
175 this.slices = new this.sliceCollection();
177 this.node = XOSModel.extend({ urlRoot: NODE_API });
178 this.nodeCollection = XOSCollection.extend({ urlRoot: NODE_API,
180 this.nodes = new this.nodeCollection();
182 this.site = XOSModel.extend({ urlRoot: SITE_API });
183 this.siteCollection = XOSCollection.extend({ urlRoot: SITE_API,
185 this.sites = new this.siteCollection();
187 this.user = XOSModel.extend({ urlRoot: USER_API });
188 this.userCollection = XOSCollection.extend({ urlRoot: USER_API,
190 this.users = new this.userCollection();
192 this.deployment = XOSModel.extend({ urlRoot: DEPLOYMENT_API });
193 this.deploymentCollection = XOSCollection.extend({ urlRoot: DEPLOYMENT_API,
194 model: this.deployment});
195 this.deployments = new this.deploymentCollection();
197 this.image = XOSModel.extend({ urlRoot: IMAGE_API });
198 this.imageCollection = XOSCollection.extend({ urlRoot: IMAGE_API,
200 this.images = new this.imageCollection();
202 this.network = XOSModel.extend({ urlRoot: NETWORK_API });
203 this.networkCollection = XOSCollection.extend({ urlRoot: NETWORK_API,
204 model: this.network});
205 this.networks = new this.networkCollection();
208 this.slicePlus = XOSModel.extend({ urlRoot: SLICEPLUS_API });
209 this.slicePlusCollection = XOSCollection.extend({ urlRoot: SLICEPLUS_API,
210 model: this.slicePlus});
211 this.slicesPlus = new this.slicePlusCollection();
213 this.listObjects = function() { return ["slivers", "slices", "nodes", "sites", "users", "deployments"]; };