Scott Baker | 1dd345e | 2014-07-07 17:06:07 -0700 | [diff] [blame] | 1 | SLIVER_API = "/plstackapi/slivers/"; |
Scott Baker | 95d6c5c | 2014-07-07 21:54:35 -0700 | [diff] [blame] | 2 | SLICE_API = "/plstackapi/slices/"; |
Scott Baker | a95caaf | 2014-07-08 00:12:59 -0700 | [diff] [blame] | 3 | NODE_API = "/plstackapi/nodes/"; |
| 4 | SITE_API = "/plstackapi/sites/"; |
| 5 | USER_API = "/plstackapi/users/"; |
| 6 | DEPLOYMENT_API = "/plstackapi/deployments"; |
Scott Baker | 1dd345e | 2014-07-07 17:06:07 -0700 | [diff] [blame] | 7 | |
Scott Baker | b7aba68 | 2014-07-07 21:30:52 -0700 | [diff] [blame] | 8 | XOSModel = Backbone.Model.extend({ |
| 9 | /* from backbone-tastypie.js */ |
Scott Baker | 7ee32a0 | 2014-07-13 09:52:15 -0700 | [diff] [blame^] | 10 | //idAttribute: 'resource_uri', |
Scott Baker | b7aba68 | 2014-07-07 21:30:52 -0700 | [diff] [blame] | 11 | |
| 12 | /* from backbone-tastypie.js */ |
| 13 | url: function() { |
Scott Baker | 7ee32a0 | 2014-07-13 09:52:15 -0700 | [diff] [blame^] | 14 | var url = this.attributes.resource_uri; |
Scott Baker | b7aba68 | 2014-07-07 21:30:52 -0700 | [diff] [blame] | 15 | |
Scott Baker | 7ee32a0 | 2014-07-13 09:52:15 -0700 | [diff] [blame^] | 16 | 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 Baker | b7aba68 | 2014-07-07 21:30:52 -0700 | [diff] [blame] | 24 | } |
| 25 | |
Scott Baker | 7ee32a0 | 2014-07-13 09:52:15 -0700 | [diff] [blame^] | 26 | // remove any existing query parameters |
| 27 | url && ( url.indexOf("?") > -1 ) && ( url = url.split("?")[0] ); |
| 28 | |
Scott Baker | b7aba68 | 2014-07-07 21:30:52 -0700 | [diff] [blame] | 29 | url && ( url += ( url.length > 0 && url.charAt( url.length - 1 ) === '/' ) ? '' : '/' ); |
| 30 | |
| 31 | url && ( url += "?no_hyperlinks=1" ); |
| 32 | |
| 33 | return url; |
| 34 | }, |
Scott Baker | 7ee32a0 | 2014-07-13 09:52:15 -0700 | [diff] [blame^] | 35 | |
| 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 Baker | b7aba68 | 2014-07-07 21:30:52 -0700 | [diff] [blame] | 45 | }); |
| 46 | |
Scott Baker | 1dd345e | 2014-07-07 17:06:07 -0700 | [diff] [blame] | 47 | XOSCollection = Backbone.Collection.extend({ |
Scott Baker | b7aba68 | 2014-07-07 21:30:52 -0700 | [diff] [blame] | 48 | objects: function() { |
| 49 | return this.models.map(function(element) { return element.attributes; }); |
| 50 | }, |
| 51 | |
Scott Baker | 1dd345e | 2014-07-07 17:06:07 -0700 | [diff] [blame] | 52 | 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 Baker | b7aba68 | 2014-07-07 21:30:52 -0700 | [diff] [blame] | 83 | model = new this.model({ |
Scott Baker | 1dd345e | 2014-07-07 17:06:07 -0700 | [diff] [blame] | 84 | resource_uri: id |
| 85 | }); |
| 86 | |
| 87 | model.fetch(options); |
Scott Baker | b7aba68 | 2014-07-07 21:30:52 -0700 | [diff] [blame] | 88 | }, |
| 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 Baker | 7ee32a0 | 2014-07-13 09:52:15 -0700 | [diff] [blame^] | 109 | |
| 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 Baker | 1dd345e | 2014-07-07 17:06:07 -0700 | [diff] [blame] | 119 | }); |
| 120 | |
| 121 | function xoslib() { |
Scott Baker | b7aba68 | 2014-07-07 21:30:52 -0700 | [diff] [blame] | 122 | 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 Baker | 95d6c5c | 2014-07-07 21:54:35 -0700 | [diff] [blame] | 126 | |
| 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 Baker | a95caaf | 2014-07-08 00:12:59 -0700 | [diff] [blame] | 131 | |
| 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 Baker | 7ee32a0 | 2014-07-13 09:52:15 -0700 | [diff] [blame^] | 151 | |
| 152 | this.listObjects = function() { return ["slivers", "slices", "nodes", "sites", "users", "deployments"]; }; |
Scott Baker | 1dd345e | 2014-07-07 17:06:07 -0700 | [diff] [blame] | 153 | }; |
| 154 | |
Scott Baker | 95d6c5c | 2014-07-07 21:54:35 -0700 | [diff] [blame] | 155 | xos = new xoslib(); |
Scott Baker | a95caaf | 2014-07-08 00:12:59 -0700 | [diff] [blame] | 156 | |