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 NETWORKTEMPLATE_API = "/plstackapi/networktemplates/";
12 NETWORK_API = "/plstackapi/networks/";
13 SERVICE_API = "/plstackapi/services/";
15 SLICEPLUS_API = "/xoslib/slicesplus/";
17 XOSModel = Backbone.Model.extend({
18 /* from backbone-tastypie.js */
19 //idAttribute: 'resource_uri',
21 /* from backbone-tastypie.js */
23 var url = this.attributes.resource_uri;
26 url = this.urlRoot + this.id;
30 // XXX I'm not sure this does anything useful
31 url = ( _.isFunction( this.collection.url ) ? this.collection.url() : this.collection.url );
32 url = url || this.urlRoot;
35 // remove any existing query parameters
36 url && ( url.indexOf("?") > -1 ) && ( url = url.split("?")[0] );
38 url && ( url += ( url.length > 0 && url.charAt( url.length - 1 ) === '/' ) ? '' : '/' );
40 url && ( url += "?no_hyperlinks=1" );
45 listMethods: function() {
47 for(var m in this) {
\r
48 if(typeof this[m] == "function") {
\r
56 XOSCollection = Backbone.Collection.extend({
58 return this.models.map(function(element) { return element.attributes; });
61 initialize: function(){
62 this.sortVar = 'name';
\r
63 this.sortOrder = 'asc';
\r
66 simpleComparator: function( model ){
\r
67 parts=this.sortVar.split(".");
\r
68 result = model.get(parts[0]);
\r
69 for (index=1; index<parts.length; ++index) {
\r
70 result=result[parts[index]];
\r
75 comparator: function (left, right) {
\r
76 var l = this.simpleComparator(left);
\r
77 var r = this.simpleComparator(right);
\r
79 if (l === void 0) return -1;
\r
80 if (r === void 0) return 1;
\r
82 if (this.sortOrder=="desc") {
\r
83 return l < r ? 1 : l > r ? -1 : 0;
\r
85 return l < r ? -1 : l > r ? 1 : 0;
\r
89 startPolling: function() {
\r
90 if (!this._polling) {
\r
92 setInterval(function() { collection.fetch(); }, 10000);
98 maybeFetch: function(options){
99 // Helper function to fetch only if this collection has not been fetched before.
101 // If this has already been fetched, call the success, if it exists
102 options.success && options.success();
103 console.log("alreadyFetched");
107 // when the original success function completes mark this collection as fetched
109 successWrapper = function(success){
111 self._fetched = true;
112 success && success.apply(this, arguments);
115 options.success = successWrapper(options.success);
116 console.log("call fetch");
120 getOrFetch: function(id, options){
121 // Helper function to use this collection as a cache for models on the server
122 var model = this.get(id);
125 options.success && options.success(model);
129 model = new this.model({
133 model.fetch(options);
136 /* from backbone-tastypie.js */
137 url: function( models ) {
138 var url = this.urlRoot || ( models && models.length && models[0].urlRoot );
139 url && ( url += ( url.length > 0 && url.charAt( url.length - 1 ) === '/' ) ? '' : '/' );
141 // Build a url to retrieve a set of models. This assume the last part of each model's idAttribute
142 // (set to 'resource_uri') contains the model's id.
143 if ( models && models.length ) {
144 var ids = _.map( models, function( model ) {
145 var parts = _.compact( model.id.split('/') );
146 return parts[ parts.length - 1 ];
148 url += 'set/' + ids.join(';') + '/';
151 url && ( url += "?no_hyperlinks=1" );
156 listMethods: function() {
158 for(var m in this) {
\r
159 if(typeof this[m] == "function") {
\r
169 this.sliver = XOSModel.extend({ urlRoot: SLIVER_API });
170 this.sliverCollection = XOSCollection.extend({ urlRoot: SLIVER_API,
171 model: this.sliver});
172 this.slivers = new this.sliverCollection();
174 this.slice = XOSModel.extend({ urlRoot: SLICE_API });
175 this.sliceCollection = XOSCollection.extend({ urlRoot: SLICE_API,
177 this.slices = new this.sliceCollection();
179 this.node = XOSModel.extend({ urlRoot: NODE_API });
180 this.nodeCollection = XOSCollection.extend({ urlRoot: NODE_API,
182 this.nodes = new this.nodeCollection();
184 this.site = XOSModel.extend({ urlRoot: SITE_API });
185 this.siteCollection = XOSCollection.extend({ urlRoot: SITE_API,
187 this.sites = new this.siteCollection();
189 this.user = XOSModel.extend({ urlRoot: USER_API });
190 this.userCollection = XOSCollection.extend({ urlRoot: USER_API,
192 this.users = new this.userCollection();
194 this.deployment = XOSModel.extend({ urlRoot: DEPLOYMENT_API });
195 this.deploymentCollection = XOSCollection.extend({ urlRoot: DEPLOYMENT_API,
196 model: this.deployment});
197 this.deployments = new this.deploymentCollection();
199 this.image = XOSModel.extend({ urlRoot: IMAGE_API });
200 this.imageCollection = XOSCollection.extend({ urlRoot: IMAGE_API,
202 this.images = new this.imageCollection();
204 this.networkTemplate = XOSModel.extend({ urlRoot: NETWORKTEMPLATE_API });
205 this.networkTemplateCollection = XOSCollection.extend({ urlRoot: NETWORKTEMPLATE_API,
206 model: this.networkTemplate});
207 this.networkTemplates = new this.networkTemplateCollection();
209 this.network = XOSModel.extend({ urlRoot: NETWORK_API });
210 this.networkCollection = XOSCollection.extend({ urlRoot: NETWORK_API,
211 model: this.network});
212 this.networks = new this.networkCollection();
214 this.service = XOSModel.extend({ urlRoot: SERVICE_API });
215 this.serviceCollection = XOSCollection.extend({ urlRoot: SERVICE_API,
216 model: this.service});
217 this.services = new this.serviceCollection();
220 this.slicePlus = XOSModel.extend({ urlRoot: SLICEPLUS_API });
221 this.slicePlusCollection = XOSCollection.extend({ urlRoot: SLICEPLUS_API,
222 model: this.slicePlus});
223 this.slicesPlus = new this.slicePlusCollection();
225 this.listObjects = function() { return ["slivers", "slices", "nodes", "sites", "users", "deployments"]; };
231 var _sync = Backbone.sync;
\r
232 Backbone.sync = function(method, model, options){
\r
233 options.beforeSend = function(xhr){
\r
234 var token = $('meta[name="csrf-token"]').attr('content');
\r
235 xhr.setRequestHeader('X-CSRFToken', token);
\r
236 console.log(token);
\r
238 return _sync(method, model, options);
\r