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";
11 SLICEPLUS_API = "/xoslib/slicesplus/";
13 XOSModel = Backbone.Model.extend({
14 /* from backbone-tastypie.js */
15 //idAttribute: 'resource_uri',
17 /* from backbone-tastypie.js */
19 var url = this.attributes.resource_uri;
22 url = this.urlRoot + this.id;
26 // XXX I'm not sure this does anything useful
27 url = ( _.isFunction( this.collection.url ) ? this.collection.url() : this.collection.url );
28 url = url || this.urlRoot;
31 // remove any existing query parameters
32 url && ( url.indexOf("?") > -1 ) && ( url = url.split("?")[0] );
34 url && ( url += ( url.length > 0 && url.charAt( url.length - 1 ) === '/' ) ? '' : '/' );
36 url && ( url += "?no_hyperlinks=1" );
41 listMethods: function() {
43 for(var m in this) {
\r
44 if(typeof this[m] == "function") {
\r
52 XOSCollection = Backbone.Collection.extend({
54 return this.models.map(function(element) { return element.attributes; });
57 initialize: function(){
58 this.sortVar = 'name';
\r
59 this.sortOrder = 'asc';
\r
62 simpleComparator: function( model ){
\r
63 parts=this.sortVar.split(".");
\r
64 result = model.get(parts[0]);
\r
65 for (index=1; index<parts.length; ++index) {
\r
66 result=result[parts[index]];
\r
71 comparator: function (left, right) {
\r
72 var l = this.simpleComparator(left);
\r
73 var r = this.simpleComparator(right);
\r
75 if (l === void 0) return -1;
\r
76 if (r === void 0) return 1;
\r
78 if (this.sortOrder=="desc") {
\r
79 return l < r ? 1 : l > r ? -1 : 0;
\r
81 return l < r ? -1 : l > r ? 1 : 0;
\r
85 startPolling: function() {
\r
88 setInterval(function() { collection.fetch(); }, 10000);
94 maybeFetch: function(options){
95 // Helper function to fetch only if this collection has not been fetched before.
97 // If this has already been fetched, call the success, if it exists
98 options.success && options.success();
99 console.log("alreadyFetched");
103 // when the original success function completes mark this collection as fetched
105 successWrapper = function(success){
107 self._fetched = true;
108 success && success.apply(this, arguments);
111 options.success = successWrapper(options.success);
112 console.log("call fetch");
116 getOrFetch: function(id, options){
117 // Helper function to use this collection as a cache for models on the server
118 var model = this.get(id);
121 options.success && options.success(model);
125 model = new this.model({
129 model.fetch(options);
132 /* from backbone-tastypie.js */
133 url: function( models ) {
134 var url = this.urlRoot || ( models && models.length && models[0].urlRoot );
135 url && ( url += ( url.length > 0 && url.charAt( url.length - 1 ) === '/' ) ? '' : '/' );
137 // Build a url to retrieve a set of models. This assume the last part of each model's idAttribute
138 // (set to 'resource_uri') contains the model's id.
139 if ( models && models.length ) {
140 var ids = _.map( models, function( model ) {
141 var parts = _.compact( model.id.split('/') );
142 return parts[ parts.length - 1 ];
144 url += 'set/' + ids.join(';') + '/';
147 url && ( url += "?no_hyperlinks=1" );
152 listMethods: function() {
154 for(var m in this) {
\r
155 if(typeof this[m] == "function") {
\r
165 this.sliver = XOSModel.extend({ urlRoot: SLIVER_API });
166 this.sliverCollection = XOSCollection.extend({ urlRoot: SLIVER_API,
167 model: this.sliver});
168 this.slivers = new this.sliverCollection();
170 this.slice = XOSModel.extend({ urlRoot: SLICE_API });
171 this.sliceCollection = XOSCollection.extend({ urlRoot: SLICE_API,
173 this.slices = new this.sliceCollection();
175 this.node = XOSModel.extend({ urlRoot: NODE_API });
176 this.nodeCollection = XOSCollection.extend({ urlRoot: NODE_API,
178 this.nodes = new this.nodeCollection();
180 this.site = XOSModel.extend({ urlRoot: SITE_API });
181 this.siteCollection = XOSCollection.extend({ urlRoot: SITE_API,
183 this.sites = new this.siteCollection();
185 this.user = XOSModel.extend({ urlRoot: USER_API });
186 this.userCollection = XOSCollection.extend({ urlRoot: USER_API,
188 this.users = new this.userCollection();
190 this.deployment = XOSModel.extend({ urlRoot: DEPLOYMENT_API });
191 this.deploymentCollection = XOSCollection.extend({ urlRoot: DEPLOYMENT_API,
192 model: this.deployment});
193 this.deployments = new this.deploymentCollection();
196 this.slicePlus = XOSModel.extend({ urlRoot: SLICEPLUS_API });
197 this.slicePlusCollection = XOSCollection.extend({ urlRoot: SLICEPLUS_API,
198 model: this.slicePlus});
199 this.slicesPlus = new this.slicePlusCollection();
201 this.listObjects = function() { return ["slivers", "slices", "nodes", "sites", "users", "deployments"]; };