You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@brooklyn.apache.org by sj...@apache.org on 2014/08/19 12:30:43 UTC

[5/9] git commit: Callers to router.js can defer actions to post-ha-status-loaded

Callers to router.js can defer actions to post-ha-status-loaded


Project: http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/commit/97d6d891
Tree: http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/tree/97d6d891
Diff: http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/diff/97d6d891

Branch: refs/heads/master
Commit: 97d6d891dc7a23691e0969ca1c7dd2b200b1aafe
Parents: 98ff36f
Author: Sam Corbett <sa...@cloudsoftcorp.com>
Authored: Fri Aug 8 16:34:29 2014 +0100
Committer: Sam Corbett <sa...@cloudsoftcorp.com>
Committed: Wed Aug 13 17:23:17 2014 +0100

----------------------------------------------------------------------
 usage/jsgui/src/main/webapp/assets/js/router.js | 41 +++++++++++++-------
 1 file changed, 27 insertions(+), 14 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/97d6d891/usage/jsgui/src/main/webapp/assets/js/router.js
----------------------------------------------------------------------
diff --git a/usage/jsgui/src/main/webapp/assets/js/router.js b/usage/jsgui/src/main/webapp/assets/js/router.js
index 029fad7..9ef8053 100644
--- a/usage/jsgui/src/main/webapp/assets/js/router.js
+++ b/usage/jsgui/src/main/webapp/assets/js/router.js
@@ -70,24 +70,37 @@ define([
         this._periodicFunctions[uid] = setInterval(periodic, interval)
     }
 
+    /**
+     * @returns {jquery.Deferred}
+     *      A promise that resolves when the high availability status has been
+     *      loaded. Actions to be taken on the view after it has loaded should
+     *      be registered with calls to .done()
+     */
     // Not just defined as a function on Router because the delay if the HA status
     // hasn't loaded requires a reference to the function, which we lose if we use
     // 'this.showView'.
-    var showViewImpl = function (router, selector, view) {
+    function showViewImpl(router, selector, view) {
         // Don't do anything until the HA status has loaded.
-        if (!ha.loaded) {
-            _.delay(showViewImpl, 100, router, selector, view);
-        } else {
-            // close the previous view - does binding clean-up and avoids memory leaks
-            if (router.currentView) {
-                router.currentView.close();
+        var promise = $.Deferred()
+            .done(function () {
+                // close the previous view - does binding clean-up and avoids memory leaks
+                if (router.currentView) {
+                    router.currentView.close();
+                }
+                // render the view inside the selector element
+                $(selector).html(view.render().el);
+                router.currentView = view;
+                return view
+            });
+        (function isComplete() {
+            if (ha.loaded) {
+                promise.resolve();
+            } else {
+                _.defer(isComplete, 100);
             }
-            // render the view inside the selector element
-            $(selector).html(view.render().el);
-            router.currentView = view;
-            return view
-        }
-    };
+        })();
+        return promise;
+    }
 
     var Router = Backbone.Router.extend({
         routes:{
@@ -106,7 +119,7 @@ define([
         },
 
         showView: function(selector, view) {
-            showViewImpl(this, selector, view);
+            return showViewImpl(this, selector, view);
         },
 
         defaultRoute: function() {