brooklyn-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From sjcorb...@apache.org
Subject [7/9] git commit: Adds inputValue and bindModelFromForm functions to brooklyn-util.js
Date Tue, 19 Aug 2014 10:30:45 GMT
Adds inputValue and bindModelFromForm functions to brooklyn-util.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/7724f121
Tree: http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/tree/7724f121
Diff: http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/diff/7724f121

Branch: refs/heads/master
Commit: 7724f1216615850a54d3d8109347e3e4a8f16392
Parents: 97d6d89
Author: Sam Corbett <sam.corbett@cloudsoftcorp.com>
Authored: Fri Aug 8 18:56:04 2014 +0100
Committer: Sam Corbett <sam.corbett@cloudsoftcorp.com>
Committed: Wed Aug 13 17:23:17 2014 +0100

----------------------------------------------------------------------
 .../webapp/assets/js/libs/brooklyn-utils.js     | 34 ++++++++++++-
 .../javascript/specs/brooklyn-utils-spec.js     | 52 +++++++++++++++++++-
 2 files changed, 82 insertions(+), 4 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/7724f121/usage/jsgui/src/main/webapp/assets/js/libs/brooklyn-utils.js
----------------------------------------------------------------------
diff --git a/usage/jsgui/src/main/webapp/assets/js/libs/brooklyn-utils.js b/usage/jsgui/src/main/webapp/assets/js/libs/brooklyn-utils.js
index 4207823..1fc8905 100644
--- a/usage/jsgui/src/main/webapp/assets/js/libs/brooklyn-utils.js
+++ b/usage/jsgui/src/main/webapp/assets/js/libs/brooklyn-utils.js
@@ -1,6 +1,6 @@
 define([
-    'underscore'
-], function (_) {
+    'jquery', 'underscore'
+], function ($, _) {
 
     var Util = {};
 
@@ -79,6 +79,36 @@ define([
         return a.pathname;
     };
 
+    /**
+     * Extracts the value of the given input. Returns true/false for for checkboxes
+     * rather than "on" or "off".
+     */
+    Util.inputValue = function($input) {
+        if ($input.attr("type") === "checkbox") {
+            return $input.is(":checked");
+        } else {
+            return $input.val();
+        }
+    };
+
+    /**
+     * Updates or initialises the given model with the values of named elements under
+     * the given element. Force-updates the model by setting silent: true.
+     */
+    Util.bindModelFromForm = function(modelOrConstructor, $el) {
+        var model = _.isFunction(modelOrConstructor) ? new modelOrConstructor() : modelOrConstructor;
+        var inputs = {};
+
+        // Look up all named elements
+        $("[name]", $el).each(function(idx, inp) {
+            var input = $(inp);
+            var name = input.attr("name");
+            inputs[name] = Util.inputValue(input);
+        });
+        model.set(inputs, { silent: true });
+        return model;
+    };
+
     return Util;
 
 });

http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/7724f121/usage/jsgui/src/test/javascript/specs/brooklyn-utils-spec.js
----------------------------------------------------------------------
diff --git a/usage/jsgui/src/test/javascript/specs/brooklyn-utils-spec.js b/usage/jsgui/src/test/javascript/specs/brooklyn-utils-spec.js
index ba9d32c..2c4012e 100644
--- a/usage/jsgui/src/test/javascript/specs/brooklyn-utils-spec.js
+++ b/usage/jsgui/src/test/javascript/specs/brooklyn-utils-spec.js
@@ -17,8 +17,8 @@
  * under the License.
  */
 define([
-    'brooklyn-utils'
-], function (Util) {
+    'brooklyn-utils', "backbone"
+], function (Util, Backbone) {
 
     describe('Rounding numbers', function () {
 
@@ -83,4 +83,52 @@ define([
             expect(Util.pathOf("/a/b/c/d#e")).toBe("/a/b/c/d");
         })
     });
+
+    describe("inputValue", function () {
+        it("should return inputs as strings", function () {
+            expect(Util.inputValue($('<input type="text" value="bob"/>'))).toBe("bob");
+            expect(Util.inputValue($('<textarea rows="10" cols="5">content</textarea>'))).toBe("content");
+        });
+
+        it("should return true/false for checkboxes", function () {
+            var input = $('<input type="checkbox" checked/>');
+            expect(Util.inputValue(input)).toBe(true);
+            input = $('<input type="checkbox" />');
+            expect(Util.inputValue(input)).toBe(false);
+        });
+    });
+
+    describe("bindModelFromForm", function () {
+        // pretend to be a Backbone model without bringing in Backbone as a dependency
+        var TestModel = Backbone.Model.extend({
+            urlRoot: function () {
+                return "/foo/bar/";
+            }
+
+        });
+        var form = $("<form>" +
+            "<input name='id' type='input' value='text'/>" +
+            "<input name='bool' type='checkbox' checked/>" +
+            "</form>");
+
+        it("should create a new model if given a constructor", function () {
+            var model = Util.bindModelFromForm(TestModel, form);
+            expect(model instanceof TestModel).toBe(true);
+            expect(model.url()).toBe("/foo/bar/text");
+            var inputs = model.attributes;
+            expect(_.keys(inputs).length).toBe(2);
+            expect(inputs.id).toBe("text");
+            expect(inputs.bool).toBe(true);
+        });
+
+        it("should update an existing model", function () {
+            var model = new TestModel({initialAttribute: "xyz"});
+            Util.bindModelFromForm(model, form);
+            var inputs = model.attributes;
+            expect(_.keys(inputs).length).toBe(3);
+            expect(inputs.id).toBe("text");
+            expect(inputs.bool).toBe(true);
+            expect(inputs.initialAttribute).toBe("xyz");
+        });
+    });
 });


Mime
View raw message