xosDeveloper view working
[plstackapi.git] / planetstack / core / xoslib / static / js / xoslib / xos-backbone.js
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";
7
8 SLICEPLUS_API = "/xoslib/slicesplus/";
9
10 XOSModel = Backbone.Model.extend({
11     /* from backbone-tastypie.js */
12     //idAttribute: 'resource_uri',
13
14     /* from backbone-tastypie.js */
15     url: function() {
16                 var url = this.attributes.resource_uri;
17
18                 if (!url) {
19                     url = this.urlRoot + this.id;
20                 }
21
22                 if (!url) {
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;
26                 }
27
28                 // remove any existing query parameters
29                 url && ( url.indexOf("?") > -1 ) && ( url = url.split("?")[0] );
30
31                 url && ( url += ( url.length > 0 && url.charAt( url.length - 1 ) === '/' ) ? '' : '/' );
32
33                 url && ( url += "?no_hyperlinks=1" );
34
35                 return url;
36         },
37
38         listMethods: function() {
39             var res = [];\r
40             for(var m in this) {\r
41                 if(typeof this[m] == "function") {\r
42                     res.push(m)\r
43                 }\r
44             }\r
45             return res;\r
46         }
47 });
48
49 XOSCollection = Backbone.Collection.extend({
50     objects: function() {
51                 return this.models.map(function(element) { return element.attributes; });
52              },
53
54     maybeFetch: function(options){
55             // Helper function to fetch only if this collection has not been fetched before.
56         if(this._fetched){
57                 // If this has already been fetched, call the success, if it exists
58             options.success && options.success();
59             console.log("alreadyFetched");
60             return;
61         }
62
63             // when the original success function completes mark this collection as fetched
64         var self = this,
65         successWrapper = function(success){
66             return function(){
67                 self._fetched = true;
68                 success && success.apply(this, arguments);
69             };
70         };
71         options.success = successWrapper(options.success);
72         console.log("call fetch");
73         this.fetch(options);
74     },
75
76     getOrFetch: function(id, options){
77             // Helper function to use this collection as a cache for models on the server
78         var model = this.get(id);
79
80         if(model){
81             options.success && options.success(model);
82             return;
83         }
84
85         model = new this.model({
86             resource_uri: id
87         });
88
89         model.fetch(options);
90     },
91
92     /* from backbone-tastypie.js */
93     url: function( models ) {
94                 var url = this.urlRoot || ( models && models.length && models[0].urlRoot );
95                 url && ( url += ( url.length > 0 && url.charAt( url.length - 1 ) === '/' ) ? '' : '/' );
96
97                 // Build a url to retrieve a set of models. This assume the last part of each model's idAttribute
98                 // (set to 'resource_uri') contains the model's id.
99                 if ( models && models.length ) {
100                         var ids = _.map( models, function( model ) {
101                                         var parts = _.compact( model.id.split('/') );
102                                         return parts[ parts.length - 1 ];
103                                 });
104                         url += 'set/' + ids.join(';') + '/';
105                 }
106
107                 url && ( url += "?no_hyperlinks=1" );
108
109                 return url;
110         },
111
112         listMethods: function() {
113             var res = [];\r
114             for(var m in this) {\r
115                 if(typeof this[m] == "function") {\r
116                     res.push(m)\r
117                 }\r
118             }\r
119             return res;\r
120         }
121 });
122
123 function xoslib() {
124     // basic REST
125     this.sliver = XOSModel.extend({ urlRoot: SLIVER_API });
126     this.sliverCollection = XOSCollection.extend({ urlRoot: SLIVER_API,
127                                                    model: this.sliver});
128     this.slivers = new this.sliverCollection();
129
130     this.slice = XOSModel.extend({ urlRoot: SLICE_API });
131     this.sliceCollection = XOSCollection.extend({ urlRoot: SLICE_API,
132                                                    model: this.slice});
133     this.slices = new this.sliceCollection();
134
135     this.node = XOSModel.extend({ urlRoot: NODE_API });
136     this.nodeCollection = XOSCollection.extend({ urlRoot: NODE_API,
137                                                    model: this.node});
138     this.nodes = new this.nodeCollection();
139
140     this.site = XOSModel.extend({ urlRoot: SITE_API });
141     this.siteCollection = XOSCollection.extend({ urlRoot: SITE_API,
142                                                    model: this.site});
143     this.sites = new this.siteCollection();
144
145     this.user = XOSModel.extend({ urlRoot: USER_API });
146     this.userCollection = XOSCollection.extend({ urlRoot: USER_API,
147                                                    model: this.user});
148     this.users = new this.userCollection();
149
150     this.deployment = XOSModel.extend({ urlRoot: DEPLOYMENT_API });
151     this.deploymentCollection = XOSCollection.extend({ urlRoot: DEPLOYMENT_API,
152                                                        model: this.deployment});
153     this.deployments = new this.deploymentCollection();
154
155     // enhanced REST
156     this.slicePlus = XOSModel.extend({ urlRoot: SLICEPLUS_API });
157     this.slicePlusCollection = XOSCollection.extend({ urlRoot: SLICEPLUS_API,
158                                                       model: this.slicePlus});
159     this.slicesPlus = new this.slicePlusCollection();
160
161     this.listObjects = function() { return ["slivers", "slices", "nodes", "sites", "users", "deployments"]; };
162 };
163
164 xos = new xoslib();
165