fix sliceplus to work with big rename
[plstackapi.git] / planetstack / core / xoslib / static / js / xoslib / xos-backbone.js
index 4b048fb..bbf13a4 100644 (file)
@@ -3,8 +3,6 @@ if (! window.XOSLIB_LOADED ) {
 
     SLIVER_API = "/plstackapi/slivers/";
     SLICE_API = "/plstackapi/slices/";
-    SLICEDEPLOYMENT_API = "/plstackapi/slice_deployments/";
-    SLICEPRIVILEGE_API = "/plstackapi/slice_privileges/";
     SLICEROLE_API = "/plstackapi/slice_roles/";
     NODE_API = "/plstackapi/nodes/";
     SITE_API = "/plstackapi/sites/";
@@ -13,10 +11,19 @@ if (! window.XOSLIB_LOADED ) {
     DEPLOYMENT_API = "/plstackapi/deployments/";
     IMAGE_API = "/plstackapi/images/";
     NETWORKTEMPLATE_API = "/plstackapi/networktemplates/";
-    NETWORKDEPLOYMENT_API = "/plstackapi/networkdeployments/";
     NETWORK_API = "/plstackapi/networks/";
     NETWORKSLIVER_API = "/plstackapi/networkslivers/";
     SERVICE_API = "/plstackapi/services/";
+    SLICEPRIVILEGE_API = "/plstackapi/slice_privileges/";
+    NETWORKDEPLOYMENT_API = "/plstackapi/networkdeployments/";
+
+    /* changed as a side effect of the big rename
+    SLICEDEPLOYMENT_API = "/plstackapi/slice_deployments/";
+    USERDEPLOYMENT_API = "/plstackapi/user_deployments/";
+    */
+
+    SLICEDEPLOYMENT_API = "/plstackapi/slicedeployments/";
+    USERDEPLOYMENT_API = "/plstackapi/userdeployments/";
 
     SLICEPLUS_API = "/xoslib/slicesplus/";
 
@@ -71,6 +78,8 @@ if (! window.XOSLIB_LOADED ) {
 
         initialize: function(){
           this.isLoaded = false;
+          this.failedLoad = false;
+          this.startedLoad = false;
           this.sortVar = 'name';\r
           this.sortOrder = 'asc';\r
           this.on( "sort", this.sorted );\r
@@ -80,7 +89,7 @@ if (! window.XOSLIB_LOADED ) {
         foreignCollections: [],\r
 \r
         sorted: function() {\r
-            this.isLoaded = true;\r
+            //console.log("sorted " + this.modelName);\r
         },\r
 \r
         simpleComparator: function( model ){\r
@@ -106,6 +115,52 @@ if (! window.XOSLIB_LOADED ) {
             }\r
         },\r
 \r
+        fetchSuccess: function(collection, response, options) {\r
+            //console.log("fetch succeeded " + collection.modelName);\r
+            this.failedLoad = false;\r
+            this.fetching = false;\r
+            if (!this.isLoaded) {\r
+                this.isLoaded = true;\r
+                Backbone.trigger("xoslib:collectionLoadChange", this);\r
+            }\r
+            this.trigger("fetchStateChange");\r
+            if (options["orig_success"]) {\r
+                options["orig_success"](collection, response, options);\r
+            }\r
+        },\r
+\r
+        fetchFailure: function(collection, response, options) {\r
+            //console.log("fetch failed " + collection.modelName);\r
+            this.fetching = false;\r
+            if ((!this.isLoaded) && (!this.failedLoad)) {\r
+                this.failedLoad=true;\r
+                Backbone.trigger("xoslib:collectionLoadChange", this);\r
+            }\r
+            this.trigger("fetchStateChange");\r
+            if (options["orig_failure"]) {\r
+                options["orig_failure"](collection, response, options);\r
+            }\r
+        },\r
+\r
+        fetch: function(options) {\r
+            var self=this;\r
+            this.fetching=true;\r
+            //console.log("fetch " + this.modelName);\r
+            if (!this.startedLoad) {\r
+                this.startedLoad=true;\r
+                Backbone.trigger("xoslib:collectionLoadChange", this);\r
+            }\r
+            this.trigger("fetchStateChange");\r
+            if (options == undefined) {\r
+                options = {};\r
+            }\r
+            options["orig_success"] = options["success"];\r
+            options["orig_failure"] = options["failure"];\r
+            options["success"] = function(collection, response, options) { self.fetchSuccess.call(self, collection, response, options); };\r
+            options["failure"] = this.fetchFailure;\r
+            Backbone.Collection.prototype.fetch.call(this, options);\r
+        },\r
+\r
         startPolling: function() {\r
             if (!this._polling) {\r
                 var collection=this;
@@ -115,6 +170,20 @@ if (! window.XOSLIB_LOADED ) {
             }
         },
 
+        refresh: function(refreshRelated) {
+            if (!this.fetching) {
+                this.fetch();
+            }
+            if (refreshRelated) {
+                for (related in this.relatedCollections) {
+                    related = xos[related];
+                    if (!related.fetching) {
+                        related.fetch();
+                    }
+                }
+            }
+        },
+
         maybeFetch: function(options){
                 // Helper function to fetch only if this collection has not been fetched before.
             if(this._fetched){
@@ -195,7 +264,11 @@ if (! window.XOSLIB_LOADED ) {
         modelName = attrs.modelName;
         modelClassName = modelName;
         collectionClassName = modelName + "Collection";
-        collectionName = modelName + "s";
+
+        if (!attrs.collectionName) {
+            attrs.collectionName = modelName + "s";
+        }
+        collectionName = attrs.collectionName;
 
         modelAttrs = {}
         collectionAttrs = {}
@@ -222,10 +295,12 @@ if (! window.XOSLIB_LOADED ) {
         lib[collectionName] = new lib[collectionClassName]();
 
         lib.allCollectionNames.push(collectionName);
+        lib.allCollections.push(lib[collectionName]);
     };
 
     function xoslib() {
         this.allCollectionNames = [];
+        this.allCollections = [];
 
         define_model(this, {urlRoot: SLIVER_API,
                             relatedCollections: {"networkSlivers": "sliver"},
@@ -293,9 +368,28 @@ if (! window.XOSLIB_LOADED ) {
         // enhanced REST
         define_model(this, {urlRoot: SLICEPLUS_API,
                             relatedCollections: {'slivers': "slice"},
-                            modelName: "slicePlus"});
+                            modelName: "slicePlus",
+                            collectionName: "slicesPlus"});
 
         this.listObjects = function() { return this.allCollectionNames; };
+
+        this.getCollectionStatus = function() {
+            stats = {isLoaded: 0, failedLoad: 0, startedLoad: 0};
+            for (index in this.allCollections) {
+                collection = this.allCollections[index];
+                if (collection.isLoaded) {
+                    stats["isLoaded"] = stats["isLoaded"] + 1;
+                }
+                if (collection.failedLoad) {
+                    stats["failedLoad"] = stats["failedLoad"] + 1;
+                }
+                if (collection.startedLoad) {
+                    stats["startedLoad"] = stats["startedLoad"] + 1;
+                }
+            }
+            stats["completedLoad"] = stats["failedLoad"] + stats["isLoaded"];
+            return stats;
+        };
     };
 
     xos = new xoslib();