brooklyn-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From sjcorb...@apache.org
Subject [2/7] git commit: Moves configuration of Backbone.View.prototype from router to view.js
Date Tue, 26 Aug 2014 10:27:08 GMT
Moves configuration of Backbone.View.prototype from router to view.js


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

Branch: refs/heads/master
Commit: b947d441958414b5eb19b6505461ef39691f8862
Parents: 33bb7a0
Author: Sam Corbett <sam.corbett@cloudsoftcorp.com>
Authored: Tue Aug 19 10:30:54 2014 +0100
Committer: Sam Corbett <sam.corbett@cloudsoftcorp.com>
Committed: Wed Aug 20 12:26:52 2014 +0100

----------------------------------------------------------------------
 usage/jsgui/src/main/webapp/assets/js/config.js |  4 +-
 .../main/webapp/assets/js/libs/brooklyn-view.js | 62 ++++++++++++++++++--
 .../src/main/webapp/assets/js/libs/brooklyn.js  |  5 --
 usage/jsgui/src/main/webapp/assets/js/router.js | 46 +--------------
 .../src/main/webapp/assets/js/view/viewutils.js |  4 +-
 .../src/test/javascript/specs/router-spec.js    |  8 +--
 6 files changed, 67 insertions(+), 62 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/b947d441/usage/jsgui/src/main/webapp/assets/js/config.js
----------------------------------------------------------------------
diff --git a/usage/jsgui/src/main/webapp/assets/js/config.js b/usage/jsgui/src/main/webapp/assets/js/config.js
index 8cff069..ef6b93c 100644
--- a/usage/jsgui/src/main/webapp/assets/js/config.js
+++ b/usage/jsgui/src/main/webapp/assets/js/config.js
@@ -80,8 +80,8 @@ require.config({
  * Inclusion of brooklyn module sets up logging.
  */
 require([
-    "backbone", "router", "brooklyn", "model/ha"
-], function (Backbone, Router, Brooklyn, ha) {
+    "backbone", "brooklyn", "router", "model/ha"
+], function (Backbone, Brooklyn, Router, ha) {
     ha.autoUpdate();
     var router = new Router();
     Backbone.history.start();

http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/b947d441/usage/jsgui/src/main/webapp/assets/js/libs/brooklyn-view.js
----------------------------------------------------------------------
diff --git a/usage/jsgui/src/main/webapp/assets/js/libs/brooklyn-view.js b/usage/jsgui/src/main/webapp/assets/js/libs/brooklyn-view.js
index 8965e81..361833e 100644
--- a/usage/jsgui/src/main/webapp/assets/js/libs/brooklyn-view.js
+++ b/usage/jsgui/src/main/webapp/assets/js/libs/brooklyn-view.js
@@ -22,11 +22,67 @@ define([
     $, _, Backbone, Util
 ) {
 
+    var module = {};
+
+    module.refresh = true;
+
+    /** Toggles automatic refreshes of instances of View. */
+    module.toggleRefresh = function () {
+        this.refresh = !this.refresh;
+        return this.refresh;
+    };
+
+    // TODO this customising of the View prototype could be expanded to include
+    // other methods from viewutils. see discussion at
+    // https://github.com/brooklyncentral/brooklyn/pull/939
+
+    // add close method to all views for clean-up
+    // (NB we have to update the prototype _here_ before any views are instantiated;
+    //  see "close" called below in "showView")
+    Backbone.View.prototype.close = function () {
+        // call user defined close method if exists
+        this.viewIsClosed = true;
+        if (_.isFunction(this.beforeClose)) {
+            this.beforeClose();
+        }
+        _.each(this._periodicFunctions, function (i) {
+            clearInterval(i);
+        });
+        this.remove();
+        this.unbind();
+    };
+
+    Backbone.View.prototype.viewIsClosed = false;
+
+    /**
+     * Registers a callback (cf setInterval) that is unregistered cleanly when the view
+     * closes. The callback is run in the context of the owning view, so callbacks can
+     * refer to 'this' safely.
+     */
+    Backbone.View.prototype.callPeriodically = function (uid, callback, interval) {
+        if (!this._periodicFunctions) {
+            this._periodicFunctions = {};
+        }
+        var old = this._periodicFunctions[uid];
+        if (old) clearInterval(old);
+
+        // Wrap callback in function that checks whether updates are enabled
+        var periodic = function () {
+            console.log(module.refresh);
+            if (module.refresh) {
+                callback.apply(this);
+            }
+        };
+        // Bind this to the view
+        periodic = _.bind(periodic, this);
+        this._periodicFunctions[uid] = setInterval(periodic, interval);
+    };
+
     /**
      * A form that listens to modifications to its inputs, maintaining a model that is
      * submitted when a button with class 'submit' is clicked.
      */
-    var GenericForm = Backbone.View.extend({
+    module.Form = Backbone.View.extend({
         events: {
             "change": "onChange",
             "submit": "onSubmit"
@@ -67,8 +123,6 @@ define([
 
     });
 
-    return {
-        Form: GenericForm
-    };
+    return module;
 
 });

http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/b947d441/usage/jsgui/src/main/webapp/assets/js/libs/brooklyn.js
----------------------------------------------------------------------
diff --git a/usage/jsgui/src/main/webapp/assets/js/libs/brooklyn.js b/usage/jsgui/src/main/webapp/assets/js/libs/brooklyn.js
index 9594d2d..9d078b9 100644
--- a/usage/jsgui/src/main/webapp/assets/js/libs/brooklyn.js
+++ b/usage/jsgui/src/main/webapp/assets/js/libs/brooklyn.js
@@ -40,11 +40,6 @@ define([
     })();
 
     var Brooklyn = {
-        refresh: true,
-        toggleRefresh: function () {
-            this.refresh = !this.refresh;
-            return this.refresh;
-        },
         view: BrooklynViews,
         util: BrooklynUtils
     };

http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/b947d441/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 c52c220..94ee839 100644
--- a/usage/jsgui/src/main/webapp/assets/js/router.js
+++ b/usage/jsgui/src/main/webapp/assets/js/router.js
@@ -26,50 +26,6 @@ define([
         HomeView, ExplorerView, CatalogView, ApidocView, ScriptGroovyView,
         HelpHtml, LabsHtml, ServerNotMasterHtml) {
 
-    // TODO this initialising - customising the View prototype - should be moved,
-    // and perhaps expanded to include other methods from viewutils
-    // see discussion at https://github.com/brooklyncentral/brooklyn/pull/939
-
-    // add close method to all views for clean-up
-    // (NB we have to update the prototype _here_ before any views are instantiated;
-    //  see "close" called below in "showView") 
-    Backbone.View.prototype.close = function () {
-        // call user defined close method if exists
-        this.viewIsClosed = true
-        if (this.beforeClose) {
-            this.beforeClose()
-        }
-        _.each(this._periodicFunctions, function(i) {
-            clearInterval(i)
-        })
-        this.remove()
-        this.unbind()
-    }
-    Backbone.View.prototype.viewIsClosed = false
-
-    /**
-     * Registers a callback (cf setInterval) that is unregistered cleanly when the view
-     * closes. The callback is run in the context of the owning view, so callbacks can
-     * refer to 'this' safely.
-     */
-    Backbone.View.prototype.callPeriodically = function (uid, callback, interval) {
-        if (!this._periodicFunctions) {
-            this._periodicFunctions = {}
-        }
-        var old = this._periodicFunctions[uid]
-        if (old) clearInterval(old)
-
-        // Wrap callback in function that checks whether updates are enabled
-        var periodic = function() {
-            if (Brooklyn.refresh) {
-                callback.apply(this);
-            }
-        };
-        // Bind this to the view
-        periodic = _.bind(periodic, this);
-        this._periodicFunctions[uid] = setInterval(periodic, interval)
-    }
-
     /**
      * @returns {jquery.Deferred}
      *      A promise that resolves when the high availability status has been
@@ -244,4 +200,4 @@ define([
     });
 
     return Router
-})
\ No newline at end of file
+})

http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/b947d441/usage/jsgui/src/main/webapp/assets/js/view/viewutils.js
----------------------------------------------------------------------
diff --git a/usage/jsgui/src/main/webapp/assets/js/view/viewutils.js b/usage/jsgui/src/main/webapp/assets/js/view/viewutils.js
index ec66285..50bb02a 100644
--- a/usage/jsgui/src/main/webapp/assets/js/view/viewutils.js
+++ b/usage/jsgui/src/main/webapp/assets/js/view/viewutils.js
@@ -299,7 +299,7 @@ define([
 //          log("getting, count "+options.count+", delay "+period+": "+url)
             
             var disabled = (options['enablement'] && !options['enablement']()) 
-                || !BrooklynConfig.refresh
+                || !BrooklynConfig.view.refresh
             if (options.count > 1 && disabled) {
                 // not enabled, just requeue
                 if (options['period']) 
@@ -426,7 +426,7 @@ define([
             var fetcher = function() {
                 if (view && view.viewIsClosed) return;
                 var disabled = (options['enablement'] && !options['enablement']())

-                    || !BrooklynConfig.refresh
+                    || !BrooklynConfig.view.refresh
                 if (options.count > 1 && disabled) {
                     // not enabled, just requeue
                     ViewUtils.fetchRepeatedlyWithDelay(view, model, options);

http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/b947d441/usage/jsgui/src/test/javascript/specs/router-spec.js
----------------------------------------------------------------------
diff --git a/usage/jsgui/src/test/javascript/specs/router-spec.js b/usage/jsgui/src/test/javascript/specs/router-spec.js
index ea39255..bd30eca 100644
--- a/usage/jsgui/src/test/javascript/specs/router-spec.js
+++ b/usage/jsgui/src/test/javascript/specs/router-spec.js
@@ -73,7 +73,7 @@ define([
 
         // Expects callback to have been called at least once
         it("should have 'this' set to the owning view", function() {
-            Brooklyn.refresh = true;
+            Brooklyn.view.refresh = true;
             var view = new CallbackView();
             waits(15);
             runs(function() {
@@ -81,13 +81,13 @@ define([
             });
         });
 
-        it("should not be run if Brooklyn.refresh is false", function() {
-            Brooklyn.refresh = false;
+        it("should not be run if Brooklyn.view.refresh is false", function() {
+            Brooklyn.view.refresh = false;
             var view = new CallbackView();
             waits(15);
             runs(function() {
                 expect(view.counter).toEqual(0);
-                Brooklyn.refresh = true;
+                Brooklyn.view.refresh = true;
             });
         });
     });


Mime
View raw message