1 if (! window.XOSLIB_LOADED ) {
2 window.XOSLIB_LOADED=true;
4 SLIVER_API = "/plstackapi/slivers/";
5 SLICE_API = "/plstackapi/slices/";
6 SLICEDEPLOYMENT_API = "/plstackapi/slice_deployments/";
7 NODE_API = "/plstackapi/nodes/";
8 SITE_API = "/plstackapi/sites/";
9 USER_API = "/plstackapi/users/";
10 DEPLOYMENT_API = "/plstackapi/deployments/";
11 IMAGE_API = "/plstackapi/images/";
12 NETWORKTEMPLATE_API = "/plstackapi/networktemplates/";
13 NETWORK_API = "/plstackapi/networks/";
14 SERVICE_API = "/plstackapi/services/";
16 SLICEPLUS_API = "/xoslib/slicesplus/";
18 XOSModel = Backbone.Model.extend({
19 /* from backbone-tastypie.js */
20 //idAttribute: 'resource_uri',
22 /* from backbone-tastypie.js */
24 var url = this.attributes.resource_uri;
27 url = this.urlRoot + this.id;
31 // XXX I'm not sure this does anything useful
32 url = ( _.isFunction( this.collection.url ) ? this.collection.url() : this.collection.url );
33 url = url || this.urlRoot;
36 // remove any existing query parameters
37 url && ( url.indexOf("?") > -1 ) && ( url = url.split("?")[0] );
39 url && ( url += ( url.length > 0 && url.charAt( url.length - 1 ) === '/' ) ? '' : '/' );
41 url && ( url += "?no_hyperlinks=1" );
46 listMethods: function() {
48 for(var m in this) {
\r
49 if(typeof this[m] == "function") {
\r
57 XOSCollection = Backbone.Collection.extend({
59 return this.models.map(function(element) { return element.attributes; });
62 initialize: function(){
63 this.sortVar = 'name';
\r
64 this.sortOrder = 'asc';
\r
67 relatedCollections: [],
\r
69 simpleComparator: function( model ){
\r
70 parts=this.sortVar.split(".");
\r
71 result = model.get(parts[0]);
\r
72 for (index=1; index<parts.length; ++index) {
\r
73 result=result[parts[index]];
\r
78 comparator: function (left, right) {
\r
79 var l = this.simpleComparator(left);
\r
80 var r = this.simpleComparator(right);
\r
82 if (l === void 0) return -1;
\r
83 if (r === void 0) return 1;
\r
85 if (this.sortOrder=="desc") {
\r
86 return l < r ? 1 : l > r ? -1 : 0;
\r
88 return l < r ? -1 : l > r ? 1 : 0;
\r
92 startPolling: function() {
\r
93 if (!this._polling) {
\r
95 setInterval(function() { collection.fetch(); }, 10000);
101 maybeFetch: function(options){
102 // Helper function to fetch only if this collection has not been fetched before.
104 // If this has already been fetched, call the success, if it exists
105 options.success && options.success();
106 console.log("alreadyFetched");
110 // when the original success function completes mark this collection as fetched
112 successWrapper = function(success){
114 self._fetched = true;
115 success && success.apply(this, arguments);
118 options.success = successWrapper(options.success);
119 console.log("call fetch");
123 getOrFetch: function(id, options){
124 // Helper function to use this collection as a cache for models on the server
125 var model = this.get(id);
128 options.success && options.success(model);
132 model = new this.model({
136 model.fetch(options);
139 filterBy: function(fieldName, value) {
140 filtered = this.filter(function(obj) {
141 return obj.get(fieldName) == value;
143 return new this.constructor(filtered);
146 /* from backbone-tastypie.js */
147 url: function( models ) {
148 var url = this.urlRoot || ( models && models.length && models[0].urlRoot );
149 url && ( url += ( url.length > 0 && url.charAt( url.length - 1 ) === '/' ) ? '' : '/' );
151 // Build a url to retrieve a set of models. This assume the last part of each model's idAttribute
152 // (set to 'resource_uri') contains the model's id.
153 if ( models && models.length ) {
154 var ids = _.map( models, function( model ) {
155 var parts = _.compact( model.id.split('/') );
156 return parts[ parts.length - 1 ];
158 url += 'set/' + ids.join(';') + '/';
161 url && ( url += "?no_hyperlinks=1" );
166 listMethods: function() {
168 for(var m in this) {
\r
169 if(typeof this[m] == "function") {
\r
176 templateHelpers: function() {
177 return { title: "foo" };
183 this.sliver = XOSModel.extend({ urlRoot: SLIVER_API });
184 this.sliverCollection = XOSCollection.extend({ urlRoot: SLIVER_API,
185 model: this.sliver});
186 this.slivers = new this.sliverCollection();
188 this.slice = XOSModel.extend({ urlRoot: SLICE_API });
189 this.sliceCollection = XOSCollection.extend({ urlRoot: SLICE_API,
190 relatedCollections: {"slivers": "slice", "sliceDeployments": "slice"},
192 this.slices = new this.sliceCollection();
194 this.sliceDeployment = XOSModel.extend({ urlRoot: SLICEDEPLOYMENT_API });
195 this.sliceDeploymentCollection = XOSCollection.extend({ urlRoot: SLICEDEPLOYMENT_API,
197 this.sliceDeployments = new this.sliceDeploymentCollection();
199 this.node = XOSModel.extend({ urlRoot: NODE_API });
200 this.nodeCollection = XOSCollection.extend({ urlRoot: NODE_API,
202 this.nodes = new this.nodeCollection();
204 this.site = XOSModel.extend({ urlRoot: SITE_API });
205 this.siteCollection = XOSCollection.extend({ urlRoot: SITE_API,
207 this.sites = new this.siteCollection();
209 this.user = XOSModel.extend({ urlRoot: USER_API });
210 this.userCollection = XOSCollection.extend({ urlRoot: USER_API,
212 this.users = new this.userCollection();
214 this.deployment = XOSModel.extend({ urlRoot: DEPLOYMENT_API });
215 this.deploymentCollection = XOSCollection.extend({ urlRoot: DEPLOYMENT_API,
216 model: this.deployment});
217 this.deployments = new this.deploymentCollection();
219 this.image = XOSModel.extend({ urlRoot: IMAGE_API });
220 this.imageCollection = XOSCollection.extend({ urlRoot: IMAGE_API,
222 this.images = new this.imageCollection();
224 this.networkTemplate = XOSModel.extend({ urlRoot: NETWORKTEMPLATE_API });
225 this.networkTemplateCollection = XOSCollection.extend({ urlRoot: NETWORKTEMPLATE_API,
226 model: this.networkTemplate});
227 this.networkTemplates = new this.networkTemplateCollection();
229 this.network = XOSModel.extend({ urlRoot: NETWORK_API });
230 this.networkCollection = XOSCollection.extend({ urlRoot: NETWORK_API,
231 model: this.network});
232 this.networks = new this.networkCollection();
234 this.service = XOSModel.extend({ urlRoot: SERVICE_API });
235 this.serviceCollection = XOSCollection.extend({ urlRoot: SERVICE_API,
236 model: this.service});
237 this.services = new this.serviceCollection();
240 this.slicePlus = XOSModel.extend({ urlRoot: SLICEPLUS_API, relatedCollections: {'slivers': "slice"} });
241 this.slicePlusCollection = XOSCollection.extend({ urlRoot: SLICEPLUS_API,
242 model: this.slicePlus});
243 this.slicesPlus = new this.slicePlusCollection();
245 this.listObjects = function() { return ["slivers", "slices", "nodes", "sites", "users", "deployments"]; };
250 function getCookie(name) {
251 var cookieValue = null;
\r
252 if (document.cookie && document.cookie != '') {
\r
253 var cookies = document.cookie.split(';');
\r
254 for (var i = 0; i < cookies.length; i++) {
\r
255 var cookie = jQuery.trim(cookies[i]);
\r
256 // Does this cookie string begin with the name we want?
\r
257 if (cookie.substring(0, name.length + 1) == (name + '=')) {
\r
258 cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
\r
263 return cookieValue;
\r
267 var _sync = Backbone.sync;
\r
268 Backbone.sync = function(method, model, options){
\r
269 options.beforeSend = function(xhr){
\r
270 var token = getCookie("csrftoken");
\r
271 xhr.setRequestHeader('X-CSRFToken', token);
\r
273 return _sync(method, model, options);
\r