progress bar on startup, and make sure stuff is loaded before we display it
[plstackapi.git] / planetstack / core / xoslib / static / js / xoslib / xos-backbone.js
index 75a3e45..36df6eb 100644 (file)
@@ -78,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
@@ -87,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
@@ -113,6 +115,43 @@ if (! window.XOSLIB_LOADED ) {
             }\r
         },\r
 \r
+        fetchSuccess: function(collection, response, options) {\r
+            this.failedLoad = false;\r
+            if (!this.isLoaded) {\r
+                this.isLoaded = true;\r
+                Backbone.trigger("xoslib:collectionLoadChange", this);\r
+            }\r
+            if (options["orig_success"]) {\r
+                options["orig_success"](collection, response, options);\r
+            }\r
+        },\r
+\r
+        fetchFailure: function(collection, response, options) {\r
+            if ((!this.isLoaded) && (!this.failedLoad)) {\r
+                this.failedLoad=true;\r
+                Backbone.trigger("xoslib:collectionLoadChange", this);\r
+            }\r
+            if (options["orig_failure"]) {\r
+                options["orig_failure"](collection, response, options);\r
+            }\r
+        },\r
+\r
+        fetch: function(options) {\r
+            var self=this;\r
+            if (!this.startedLoad) {\r
+                this.startedLoad=true;\r
+                Backbone.trigger("xoslib:collectionLoadChange", this);\r
+            }\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;
@@ -229,10 +268,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"},
@@ -303,6 +344,24 @@ if (! window.XOSLIB_LOADED ) {
                             modelName: "slicePlus"});
 
         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();