1 SLIVER_API = "/plstackapi/slivers/";
2 SLICE_API = "/plstackapi/slices/";
3 NODE_API = "/plstackapi/nodes/";
4 SITE_API = "/plstackapi/sites/";
5 USER_API = "/plstackapi/users/";
6 DEPLOYMENT_API = "/plstackapi/deployments";
8 SLICEPLUS_API = "/xoslib/slicesplus/";
10 XOSModel = Backbone.Model.extend({
11 /* from backbone-tastypie.js */
12 //idAttribute: 'resource_uri',
14 /* from backbone-tastypie.js */
16 var url = this.attributes.resource_uri;
19 url = this.urlRoot + this.id;
23 // XXX I'm not sure this does anything useful
24 url = ( _.isFunction( this.collection.url ) ? this.collection.url() : this.collection.url );
25 url = url || this.urlRoot;
28 // remove any existing query parameters
29 url && ( url.indexOf("?") > -1 ) && ( url = url.split("?")[0] );
31 url && ( url += ( url.length > 0 && url.charAt( url.length - 1 ) === '/' ) ? '' : '/' );
33 url && ( url += "?no_hyperlinks=1" );
38 listMethods: function() {
40 for(var m in this) {
\r
41 if(typeof this[m] == "function") {
\r
49 XOSCollection = Backbone.Collection.extend({
51 return this.models.map(function(element) { return element.attributes; });
54 initialize: function(){
55 this.sortVar = 'name';
\r
56 this.sortOrder = 'asc';
\r
59 simpleComparator: function( model ){
\r
60 parts=this.sortVar.split(".");
\r
61 result = model.get(parts[0]);
\r
62 for (index=1; index<parts.length; ++index) {
\r
63 result=result[parts[index]];
\r
68 comparator: function (left, right) {
\r
69 var l = this.simpleComparator(left);
\r
70 var r = this.simpleComparator(right);
\r
72 if (l === void 0) return -1;
\r
73 if (r === void 0) return 1;
\r
75 if (this.sortOrder=="desc") {
\r
76 return l < r ? 1 : l > r ? -1 : 0;
\r
78 return l < r ? -1 : l > r ? 1 : 0;
\r
82 startPolling: function() {
\r
85 setInterval(function() { collection.fetch(); }, 10000);
91 maybeFetch: function(options){
92 // Helper function to fetch only if this collection has not been fetched before.
94 // If this has already been fetched, call the success, if it exists
95 options.success && options.success();
96 console.log("alreadyFetched");
100 // when the original success function completes mark this collection as fetched
102 successWrapper = function(success){
104 self._fetched = true;
105 success && success.apply(this, arguments);
108 options.success = successWrapper(options.success);
109 console.log("call fetch");
113 getOrFetch: function(id, options){
114 // Helper function to use this collection as a cache for models on the server
115 var model = this.get(id);
118 options.success && options.success(model);
122 model = new this.model({
126 model.fetch(options);
129 /* from backbone-tastypie.js */
130 url: function( models ) {
131 var url = this.urlRoot || ( models && models.length && models[0].urlRoot );
132 url && ( url += ( url.length > 0 && url.charAt( url.length - 1 ) === '/' ) ? '' : '/' );
134 // Build a url to retrieve a set of models. This assume the last part of each model's idAttribute
135 // (set to 'resource_uri') contains the model's id.
136 if ( models && models.length ) {
137 var ids = _.map( models, function( model ) {
138 var parts = _.compact( model.id.split('/') );
139 return parts[ parts.length - 1 ];
141 url += 'set/' + ids.join(';') + '/';
144 url && ( url += "?no_hyperlinks=1" );
149 listMethods: function() {
151 for(var m in this) {
\r
152 if(typeof this[m] == "function") {
\r
162 this.sliver = XOSModel.extend({ urlRoot: SLIVER_API });
163 this.sliverCollection = XOSCollection.extend({ urlRoot: SLIVER_API,
164 model: this.sliver});
165 this.slivers = new this.sliverCollection();
167 this.slice = XOSModel.extend({ urlRoot: SLICE_API });
168 this.sliceCollection = XOSCollection.extend({ urlRoot: SLICE_API,
170 this.slices = new this.sliceCollection();
172 this.node = XOSModel.extend({ urlRoot: NODE_API });
173 this.nodeCollection = XOSCollection.extend({ urlRoot: NODE_API,
175 this.nodes = new this.nodeCollection();
177 this.site = XOSModel.extend({ urlRoot: SITE_API });
178 this.siteCollection = XOSCollection.extend({ urlRoot: SITE_API,
180 this.sites = new this.siteCollection();
182 this.user = XOSModel.extend({ urlRoot: USER_API });
183 this.userCollection = XOSCollection.extend({ urlRoot: USER_API,
185 this.users = new this.userCollection();
187 this.deployment = XOSModel.extend({ urlRoot: DEPLOYMENT_API });
188 this.deploymentCollection = XOSCollection.extend({ urlRoot: DEPLOYMENT_API,
189 model: this.deployment});
190 this.deployments = new this.deploymentCollection();
193 this.slicePlus = XOSModel.extend({ urlRoot: SLICEPLUS_API });
194 this.slicePlusCollection = XOSCollection.extend({ urlRoot: SLICEPLUS_API,
195 model: this.slicePlus});
196 this.slicesPlus = new this.slicePlusCollection();
198 this.listObjects = function() { return ["slivers", "slices", "nodes", "sites", "users", "deployments"]; };