blob: b113535992fab9695f90e73fb1406ab96dd2b0b5 [file] [log] [blame]
Scott Baker1dd345e2014-07-07 17:06:07 -07001SLIVER_API = "/plstackapi/slivers/";
Scott Baker95d6c5c2014-07-07 21:54:35 -07002SLICE_API = "/plstackapi/slices/";
Scott Bakera95caaf2014-07-08 00:12:59 -07003NODE_API = "/plstackapi/nodes/";
4SITE_API = "/plstackapi/sites/";
5USER_API = "/plstackapi/users/";
6DEPLOYMENT_API = "/plstackapi/deployments";
Scott Baker1dd345e2014-07-07 17:06:07 -07007
Scott Bakerb7aba682014-07-07 21:30:52 -07008XOSModel = Backbone.Model.extend({
9 /* from backbone-tastypie.js */
Scott Baker7ee32a02014-07-13 09:52:15 -070010 //idAttribute: 'resource_uri',
Scott Bakerb7aba682014-07-07 21:30:52 -070011
12 /* from backbone-tastypie.js */
13 url: function() {
Scott Baker7ee32a02014-07-13 09:52:15 -070014 var url = this.attributes.resource_uri;
Scott Bakerb7aba682014-07-07 21:30:52 -070015
Scott Baker7ee32a02014-07-13 09:52:15 -070016 if (!url) {
17 url = this.urlRoot + this.id;
18 }
19
20 if (!url) {
21 // XXX I'm not sure this does anything useful
22 url = ( _.isFunction( this.collection.url ) ? this.collection.url() : this.collection.url );
23 url = url || this.urlRoot;
Scott Bakerb7aba682014-07-07 21:30:52 -070024 }
25
Scott Baker7ee32a02014-07-13 09:52:15 -070026 // remove any existing query parameters
27 url && ( url.indexOf("?") > -1 ) && ( url = url.split("?")[0] );
28
Scott Bakerb7aba682014-07-07 21:30:52 -070029 url && ( url += ( url.length > 0 && url.charAt( url.length - 1 ) === '/' ) ? '' : '/' );
30
31 url && ( url += "?no_hyperlinks=1" );
32
33 return url;
34 },
Scott Baker7ee32a02014-07-13 09:52:15 -070035
36 listMethods: function() {
37 var res = [];
38 for(var m in this) {
39 if(typeof this[m] == "function") {
40 res.push(m)
41 }
42 }
43 return res;
44 }
Scott Bakerb7aba682014-07-07 21:30:52 -070045});
46
Scott Baker1dd345e2014-07-07 17:06:07 -070047XOSCollection = Backbone.Collection.extend({
Scott Bakerb7aba682014-07-07 21:30:52 -070048 objects: function() {
49 return this.models.map(function(element) { return element.attributes; });
50 },
51
Scott Baker1dd345e2014-07-07 17:06:07 -070052 maybeFetch: function(options){
53 // Helper function to fetch only if this collection has not been fetched before.
54 if(this._fetched){
55 // If this has already been fetched, call the success, if it exists
56 options.success && options.success();
57 console.log("alreadyFetched");
58 return;
59 }
60
61 // when the original success function completes mark this collection as fetched
62 var self = this,
63 successWrapper = function(success){
64 return function(){
65 self._fetched = true;
66 success && success.apply(this, arguments);
67 };
68 };
69 options.success = successWrapper(options.success);
70 console.log("call fetch");
71 this.fetch(options);
72 },
73
74 getOrFetch: function(id, options){
75 // Helper function to use this collection as a cache for models on the server
76 var model = this.get(id);
77
78 if(model){
79 options.success && options.success(model);
80 return;
81 }
82
Scott Bakerb7aba682014-07-07 21:30:52 -070083 model = new this.model({
Scott Baker1dd345e2014-07-07 17:06:07 -070084 resource_uri: id
85 });
86
87 model.fetch(options);
Scott Bakerb7aba682014-07-07 21:30:52 -070088 },
89
90 /* from backbone-tastypie.js */
91 url: function( models ) {
92 var url = this.urlRoot || ( models && models.length && models[0].urlRoot );
93 url && ( url += ( url.length > 0 && url.charAt( url.length - 1 ) === '/' ) ? '' : '/' );
94
95 // Build a url to retrieve a set of models. This assume the last part of each model's idAttribute
96 // (set to 'resource_uri') contains the model's id.
97 if ( models && models.length ) {
98 var ids = _.map( models, function( model ) {
99 var parts = _.compact( model.id.split('/') );
100 return parts[ parts.length - 1 ];
101 });
102 url += 'set/' + ids.join(';') + '/';
103 }
104
105 url && ( url += "?no_hyperlinks=1" );
106
107 return url;
108 },
Scott Baker7ee32a02014-07-13 09:52:15 -0700109
110 listMethods: function() {
111 var res = [];
112 for(var m in this) {
113 if(typeof this[m] == "function") {
114 res.push(m)
115 }
116 }
117 return res;
118 }
Scott Baker1dd345e2014-07-07 17:06:07 -0700119});
120
121function xoslib() {
Scott Bakerb7aba682014-07-07 21:30:52 -0700122 this.sliver = XOSModel.extend({ urlRoot: SLIVER_API });
123 this.sliverCollection = XOSCollection.extend({ urlRoot: SLIVER_API,
124 model: this.sliver});
125 this.slivers = new this.sliverCollection();
Scott Baker95d6c5c2014-07-07 21:54:35 -0700126
127 this.slice = XOSModel.extend({ urlRoot: SLICE_API });
128 this.sliceCollection = XOSCollection.extend({ urlRoot: SLICE_API,
129 model: this.slice});
130 this.slices = new this.sliceCollection();
Scott Bakera95caaf2014-07-08 00:12:59 -0700131
132 this.node = XOSModel.extend({ urlRoot: NODE_API });
133 this.nodeCollection = XOSCollection.extend({ urlRoot: NODE_API,
134 model: this.node});
135 this.nodes = new this.nodeCollection();
136
137 this.site = XOSModel.extend({ urlRoot: SITE_API });
138 this.siteCollection = XOSCollection.extend({ urlRoot: SITE_API,
139 model: this.site});
140 this.sites = new this.siteCollection();
141
142 this.user = XOSModel.extend({ urlRoot: USER_API });
143 this.userCollection = XOSCollection.extend({ urlRoot: USER_API,
144 model: this.user});
145 this.users = new this.userCollection();
146
147 this.deployment = XOSModel.extend({ urlRoot: DEPLOYMENT_API });
148 this.deploymentCollection = XOSCollection.extend({ urlRoot: DEPLOYMENT_API,
149 model: this.deployment});
150 this.deployments = new this.deploymentCollection();
Scott Baker7ee32a02014-07-13 09:52:15 -0700151
152 this.listObjects = function() { return ["slivers", "slices", "nodes", "sites", "users", "deployments"]; };
Scott Baker1dd345e2014-07-07 17:06:07 -0700153};
154
Scott Baker95d6c5c2014-07-07 21:54:35 -0700155xos = new xoslib();
Scott Bakera95caaf2014-07-08 00:12:59 -0700156