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:44 UTC
[6/9] git commit: jsgui: Adds brooklyn views module
jsgui: Adds brooklyn views module
Contains GenericForm object that binds inputs to given model
Project: http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/commit/002f70f3
Tree: http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/tree/002f70f3
Diff: http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/diff/002f70f3
Branch: refs/heads/master
Commit: 002f70f35c20a55e2f41cbdcb4cec40870ea7c0f
Parents: a1b6a0d
Author: Sam Corbett <sa...@cloudsoftcorp.com>
Authored: Tue Aug 12 15:10:43 2014 +0100
Committer: Sam Corbett <sa...@cloudsoftcorp.com>
Committed: Wed Aug 13 17:23:17 2014 +0100
----------------------------------------------------------------------
usage/jsgui/src/main/webapp/assets/js/config.js | 1 +
.../main/webapp/assets/js/libs/brooklyn-view.js | 74 ++++++++++++++++++
.../src/main/webapp/assets/js/libs/brooklyn.js | 15 ++--
usage/jsgui/src/test/javascript/config.txt | 1 +
.../src/test/javascript/specs/brooklyn-spec.js | 81 ++++++++++++++++++++
5 files changed, 166 insertions(+), 6 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/002f70f3/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 6a7d6df..8cff069 100644
--- a/usage/jsgui/src/main/webapp/assets/js/config.js
+++ b/usage/jsgui/src/main/webapp/assets/js/config.js
@@ -39,6 +39,7 @@ require.config({
"moment":"libs/moment.min",
"handlebars":"libs/handlebars-1.0.rc.1",
"brooklyn":"libs/brooklyn",
+ "brooklyn-view":"libs/brooklyn-view",
"brooklyn-utils":"libs/brooklyn-utils",
"datatables-extensions":"libs/dataTables.extensions",
"googlemaps":"view/googlemaps",
http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/002f70f3/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
new file mode 100644
index 0000000..8965e81
--- /dev/null
+++ b/usage/jsgui/src/main/webapp/assets/js/libs/brooklyn-view.js
@@ -0,0 +1,74 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+*/
+define([
+ "jquery", "underscore", "backbone", "brooklyn-utils"
+], function (
+ $, _, Backbone, Util
+) {
+
+ /**
+ * 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({
+ events: {
+ "change": "onChange",
+ "submit": "onSubmit"
+ },
+
+ initialize: function() {
+ if (!this.options.template) {
+ throw new Error("template required by GenericForm");
+ } else if (!this.options.onSubmit) {
+ throw new Error("onSubmit function required by GenericForm");
+ }
+ this.onSubmitCallback = this.options.onSubmit;
+ this.template = _.template(this.options.template);
+ this.model = new (this.options.model || Backbone.Model);
+ _.bindAll(this, "onSubmit", "onChange");
+ this.render();
+ },
+
+ render: function() {
+ this.$el.html(this.template());
+ // Initialise the model with existing values
+ Util.bindModelFromForm(this.model, this.$el);
+ return this;
+ },
+
+ onChange: function(e) {
+ var target = $(e.target);
+ var name = target.attr("name");
+ this.model.set(name, Util.inputValue(target), { silent: true });
+ },
+
+ onSubmit: function(e) {
+ e.preventDefault();
+ // TODO: Could validate model
+ this.onSubmitCallback(this.model);
+ return false;
+ }
+
+ });
+
+ return {
+ Form: GenericForm
+ };
+
+});
http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/002f70f3/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 d8abd14..9594d2d 100644
--- a/usage/jsgui/src/main/webapp/assets/js/libs/brooklyn.js
+++ b/usage/jsgui/src/main/webapp/assets/js/libs/brooklyn.js
@@ -1,6 +1,7 @@
/** Client configuration. */
define([
-], function () {
+ "brooklyn-view", "brooklyn-utils"
+], function (BrooklynViews, BrooklynUtils) {
/**
* Makes the console API safe to use:
@@ -38,13 +39,15 @@ define([
}
})();
- var Config = {
- "refresh":true,
- "toggleRefresh":function () {
+ var Brooklyn = {
+ refresh: true,
+ toggleRefresh: function () {
this.refresh = !this.refresh;
return this.refresh;
- }
+ },
+ view: BrooklynViews,
+ util: BrooklynUtils
};
- return Config;
+ return Brooklyn;
});
http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/002f70f3/usage/jsgui/src/test/javascript/config.txt
----------------------------------------------------------------------
diff --git a/usage/jsgui/src/test/javascript/config.txt b/usage/jsgui/src/test/javascript/config.txt
index 8bf0b2b..369c404 100644
--- a/usage/jsgui/src/test/javascript/config.txt
+++ b/usage/jsgui/src/test/javascript/config.txt
@@ -16,6 +16,7 @@
"moment":"js/libs/moment.min",
"handlebars":"js/libs/handlebars-1.0.rc.1",
"brooklyn":"js/libs/brooklyn",
+ "brooklyn-view":"js/libs/brooklyn-view",
"brooklyn-utils":"js/libs/brooklyn-utils",
"datatables-extensions":"js/libs/dataTables.extensions",
"googlemaps":"view/googlemaps",
http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/002f70f3/usage/jsgui/src/test/javascript/specs/brooklyn-spec.js
----------------------------------------------------------------------
diff --git a/usage/jsgui/src/test/javascript/specs/brooklyn-spec.js b/usage/jsgui/src/test/javascript/specs/brooklyn-spec.js
new file mode 100644
index 0000000..8c9642d
--- /dev/null
+++ b/usage/jsgui/src/test/javascript/specs/brooklyn-spec.js
@@ -0,0 +1,81 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+define([
+ "brooklyn", "backbone"
+], function (B, Backbone) {
+
+ describe("view", function () {
+ describe("form", function() {
+ var formTemplate = '<form>' +
+ '<input name="id" type="text"/>' +
+ '<input name="initialvalue" type="text" value="present"/>' +
+ '<button type="submit" class="submit">Submit</button>' +
+ '</form>';
+
+ it("should set existing values on the model", function() {
+ var form = new B.view.Form({template: formTemplate, onSubmit: function() {}});
+ expect(form.model.get("initialvalue")).toBe("present");
+ expect(form.model.get("id")).toBe("");
+ });
+
+ it("should maintain a model as inputs change", function () {
+ var form = new B.view.Form({
+ template: formTemplate,
+ onSubmit: function() {}
+ });
+ // simulate id entry
+ form.$("[name=id]").val("987");
+ form.$("[name=id]").trigger("change");
+ expect(form.model.get("id")).toBe("987");
+ });
+
+ it("should call the onSubmit callback when the form is submitted", function () {
+ var wasCalled = false;
+ var onSubmit = function (model) {
+ wasCalled = true;
+ };
+ var form = new B.view.Form({
+ template: formTemplate,
+ onSubmit: onSubmit
+ });
+ console.log(form.$(".submit"));
+ form.$("form").trigger("submit");
+ expect(wasCalled).toBe(true);
+ });
+
+ // todo: expect failure jasmine method?
+ it("should fail if called without template or onSubmit", function () {
+ try {
+ new B.view.Form({template: ""});
+ fail;
+ } catch (e) {
+ // expected
+ }
+ try {
+ new B.view.Form({onSubmit: function() {}});
+ fail;
+ } catch (e) {
+ // expected
+ }
+
+ });
+ });
+
+ });
+});
\ No newline at end of file