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";
12 SLICEPLUS_API = "/xoslib/slicesplus/";
14 XOSModel = Backbone.Model.extend({
15 /* from backbone-tastypie.js */
16 //idAttribute: 'resource_uri',
18 /* from backbone-tastypie.js */
20 var url = this.attributes.resource_uri;
23 url = this.urlRoot + this.id;
27 // XXX I'm not sure this does anything useful
28 url = ( _.isFunction( this.collection.url ) ? this.collection.url() : this.collection.url );
29 url = url || this.urlRoot;
32 // remove any existing query parameters
33 url && ( url.indexOf("?") > -1 ) && ( url = url.split("?")[0] );
35 url && ( url += ( url.length > 0 && url.charAt( url.length - 1 ) === '/' ) ? '' : '/' );
37 url && ( url += "?no_hyperlinks=1" );
42 listMethods: function() {
44 for(var m in this) {
\r
45 if(typeof this[m] == "function") {
\r
53 XOSCollection = Backbone.Collection.extend({
55 return this.models.map(function(element) { return element.attributes; });
58 initialize: function(){
59 this.sortVar = 'name';
\r
60 this.sortOrder = 'asc';
\r
63 simpleComparator: function( model ){
\r
64 parts=this.sortVar.split(".");
\r
65 result = model.get(parts[0]);
\r
66 for (index=1; index<parts.length; ++index) {
\r
67 result=result[parts[index]];
\r
72 comparator: function (left, right) {
\r
73 var l = this.simpleComparator(left);
\r
74 var r = this.simpleComparator(right);
\r
76 if (l === void 0) return -1;
\r
77 if (r === void 0) return 1;
\r
79 if (this.sortOrder=="desc") {
\r
80 return l < r ? 1 : l > r ? -1 : 0;
\r
82 return l < r ? -1 : l > r ? 1 : 0;
\r
86 startPolling: function() {
\r
89 setInterval(function() { collection.fetch(); }, 10000);
95 maybeFetch: function(options){
96 // Helper function to fetch only if this collection has not been fetched before.
98 // If this has already been fetched, call the success, if it exists
99 options.success && options.success();
100 console.log("alreadyFetched");
104 // when the original success function completes mark this collection as fetched
106 successWrapper = function(success){
108 self._fetched = true;
109 success && success.apply(this, arguments);
112 options.success = successWrapper(options.success);
113 console.log("call fetch");
117 getOrFetch: function(id, options){
118 // Helper function to use this collection as a cache for models on the server
119 var model = this.get(id);
122 options.success && options.success(model);
126 model = new this.model({
130 model.fetch(options);
133 /* from backbone-tastypie.js */
134 url: function( models ) {
135 var url = this.urlRoot || ( models && models.length && models[0].urlRoot );
136 url && ( url += ( url.length > 0 && url.charAt( url.length - 1 ) === '/' ) ? '' : '/' );
138 // Build a url to retrieve a set of models. This assume the last part of each model's idAttribute
139 // (set to 'resource_uri') contains the model's id.
140 if ( models && models.length ) {
141 var ids = _.map( models, function( model ) {
142 var parts = _.compact( model.id.split('/') );
143 return parts[ parts.length - 1 ];
145 url += 'set/' + ids.join(';') + '/';
148 url && ( url += "?no_hyperlinks=1" );
153 listMethods: function() {
155 for(var m in this) {
\r
156 if(typeof this[m] == "function") {
\r
166 this.sliver = XOSModel.extend({ urlRoot: SLIVER_API });
167 this.sliverCollection = XOSCollection.extend({ urlRoot: SLIVER_API,
168 model: this.sliver});
169 this.slivers = new this.sliverCollection();
171 this.slice = XOSModel.extend({ urlRoot: SLICE_API });
172 this.sliceCollection = XOSCollection.extend({ urlRoot: SLICE_API,
174 this.slices = new this.sliceCollection();
176 this.node = XOSModel.extend({ urlRoot: NODE_API });
177 this.nodeCollection = XOSCollection.extend({ urlRoot: NODE_API,
179 this.nodes = new this.nodeCollection();
181 this.site = XOSModel.extend({ urlRoot: SITE_API });
182 this.siteCollection = XOSCollection.extend({ urlRoot: SITE_API,
184 this.sites = new this.siteCollection();
186 this.user = XOSModel.extend({ urlRoot: USER_API });
187 this.userCollection = XOSCollection.extend({ urlRoot: USER_API,
189 this.users = new this.userCollection();
191 this.deployment = XOSModel.extend({ urlRoot: DEPLOYMENT_API });
192 this.deploymentCollection = XOSCollection.extend({ urlRoot: DEPLOYMENT_API,
193 model: this.deployment});
194 this.deployments = new this.deploymentCollection();
196 this.image = XOSModel.extend({ urlRoot: IMAGE_API });
197 this.imageCollection = XOSCollection.extend({ urlRoot: IMAGE_API,
199 this.images = new this.imageCollection();
202 this.slicePlus = XOSModel.extend({ urlRoot: SLICEPLUS_API });
203 this.slicePlusCollection = XOSCollection.extend({ urlRoot: SLICEPLUS_API,
204 model: this.slicePlus});
205 this.slicesPlus = new this.slicePlusCollection();
207 this.listObjects = function() { return ["slivers", "slices", "nodes", "sites", "users", "deployments"]; };