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 XOSModel = Backbone.Model.extend({
9 /* from backbone-tastypie.js */
10 idAttribute: 'resource_uri',
12 /* from backbone-tastypie.js */
14 // Use the id if possible
17 // If there's no idAttribute, try to have the collection construct a url. Fallback to 'urlRoot'.
19 url = this.collection && ( _.isFunction( this.collection.url ) ? this.collection.url() : this.collection.url );
21 url = url || this.urlRoot;
24 url && ( url += ( url.length > 0 && url.charAt( url.length - 1 ) === '/' ) ? '' : '/' );
26 url && ( url += "?no_hyperlinks=1" );
32 XOSCollection = Backbone.Collection.extend({
34 return this.models.map(function(element) { return element.attributes; });
37 maybeFetch: function(options){
38 // Helper function to fetch only if this collection has not been fetched before.
40 // If this has already been fetched, call the success, if it exists
41 options.success && options.success();
42 console.log("alreadyFetched");
46 // when the original success function completes mark this collection as fetched
48 successWrapper = function(success){
51 success && success.apply(this, arguments);
54 options.success = successWrapper(options.success);
55 console.log("call fetch");
59 getOrFetch: function(id, options){
60 // Helper function to use this collection as a cache for models on the server
61 var model = this.get(id);
64 options.success && options.success(model);
68 model = new this.model({
75 /* from backbone-tastypie.js */
76 url: function( models ) {
77 var url = this.urlRoot || ( models && models.length && models[0].urlRoot );
78 url && ( url += ( url.length > 0 && url.charAt( url.length - 1 ) === '/' ) ? '' : '/' );
80 // Build a url to retrieve a set of models. This assume the last part of each model's idAttribute
81 // (set to 'resource_uri') contains the model's id.
82 if ( models && models.length ) {
83 var ids = _.map( models, function( model ) {
84 var parts = _.compact( model.id.split('/') );
85 return parts[ parts.length - 1 ];
87 url += 'set/' + ids.join(';') + '/';
90 url && ( url += "?no_hyperlinks=1" );
97 this.sliver = XOSModel.extend({ urlRoot: SLIVER_API });
98 this.sliverCollection = XOSCollection.extend({ urlRoot: SLIVER_API,
100 this.slivers = new this.sliverCollection();
102 this.slice = XOSModel.extend({ urlRoot: SLICE_API });
103 this.sliceCollection = XOSCollection.extend({ urlRoot: SLICE_API,
105 this.slices = new this.sliceCollection();
107 this.node = XOSModel.extend({ urlRoot: NODE_API });
108 this.nodeCollection = XOSCollection.extend({ urlRoot: NODE_API,
110 this.nodes = new this.nodeCollection();
112 this.site = XOSModel.extend({ urlRoot: SITE_API });
113 this.siteCollection = XOSCollection.extend({ urlRoot: SITE_API,
115 this.sites = new this.siteCollection();
117 this.user = XOSModel.extend({ urlRoot: USER_API });
118 this.userCollection = XOSCollection.extend({ urlRoot: USER_API,
120 this.users = new this.userCollection();
122 this.deployment = XOSModel.extend({ urlRoot: DEPLOYMENT_API });
123 this.deploymentCollection = XOSCollection.extend({ urlRoot: DEPLOYMENT_API,
124 model: this.deployment});
125 this.deployments = new this.deploymentCollection();