99f0784a9c75347d4ad893ae95c22759877020cb
[plstackapi.git] / planetstack / core / xoslib / static / js / xos-backbone.js
1 SLIVER_API = "/plstackapi/slivers/";
2
3 XOSCollection = Backbone.Collection.extend({
4     maybeFetch: function(options){
5             // Helper function to fetch only if this collection has not been fetched before.
6         if(this._fetched){
7                 // If this has already been fetched, call the success, if it exists
8             options.success && options.success();
9             console.log("alreadyFetched");
10             return;
11         }
12
13             // when the original success function completes mark this collection as fetched
14         var self = this,
15         successWrapper = function(success){
16             return function(){
17                 self._fetched = true;
18                 success && success.apply(this, arguments);
19             };
20         };
21         options.success = successWrapper(options.success);
22         console.log("call fetch");
23         this.fetch(options);
24     },
25
26     getOrFetch: function(id, options){
27             // Helper function to use this collection as a cache for models on the server
28         var model = this.get(id);
29
30         if(model){
31             options.success && options.success(model);
32             return;
33         }
34
35         model = new Sliver({
36             resource_uri: id
37         });
38
39         model.fetch(options);
40     }
41 });
42
43 function xoslib() {
44     this.sliver = Backbone.Model.extend({ urlRoot: SLIVER_API });
45     this.slivers = XOSCollection.extend({ urlRoot: SLIVER_API,
46                                     model: this.sliver});
47 };
48
49 XOSLib = new xoslib();