Permalink
Browse files

Moved #fieldValues method to always be present on jquery objects.

  • Loading branch information...
1 parent 7ec62b3 commit 7583ee4b7426841dec14744990c5e84995de578c @nathansobo committed Feb 16, 2011
@@ -10,6 +10,26 @@ jQuery.fn.extend({
return this.data('view');
},
+ fieldValues: function() {
+ var values = {};
+ this.find("input,select,textarea").each(function() {
+ var elt = jQuery(this);
+ var name = elt.attr('name');
+ if (!name) return;
+ if (elt.is(':checkbox')) {
+ values[name] = elt.attr('checked');
+ } else {
+ values[name] = elt.val();
+ }
+ });
+
+ if (this.customFieldValues) {
+ jQuery.extend(values, this.customFieldValues());
+ }
+
+ return values;
+ },
+
bindHtml: function(record, fieldName) {
var subscription = this.data('bindHtmlSubscription');
if (subscription) subscription.destroy();
@@ -71,26 +71,6 @@ _.constructor("Monarch.View.Template", {
}, this);
},
- fieldValues: function() {
- var values = {};
- this.find("input,select,textarea").each(function() {
- var elt = jQuery(this);
- var name = elt.attr('name');
- if (!name) return;
- if (elt.is(':checkbox')) {
- values[name] = elt.attr('checked');
- } else {
- values[name] = elt.val();
- }
- });
-
- if (this.customFieldValues) {
- jQuery.extend(values, this.customFieldValues());
- }
-
- return values;
- },
-
fieldValuesMatchModel: function() {
var model = this.model();
return _.every(this.fieldValues(), function(value, fieldName) {
@@ -40,6 +40,54 @@ Screw.Unit(function(c) { with(c) {
});
});
+
+ describe("jQuery.fn.fieldValues()", function() {
+ var view;
+ before(function() {
+ view = Monarch.View.build(function(b) { with(b) {
+ div(function() {
+ input({name: "foo", value: "Foo"}).ref('foo');
+ input({name: "bar", value: "Bar"}).ref('bar');
+ input({name: "baz", type: "checkbox", checked: false}).ref('baz');
+ input({value: "Do not include because I have no name"});
+ textarea({name: 'textarea'}, "This too");
+
+ select({name: "quux"}, function() {
+ option({value: "1"});
+ option({value: "2", selected: 1});
+ option({value: "3"});
+ }).ref('quux');
+ });
+ }});
+ });
+
+ it("returns a hash of name value pairs for all input elements on the view", function() {
+ expect(view.fieldValues()).to(equal, {
+ foo: "Foo",
+ bar: "Bar",
+ baz: false,
+ textarea: "This too",
+ quux: '2'
+ });
+ });
+
+ it("if a customFieldValues method is present, merges its results into the returned fieldValues", function() {
+ view.customFieldValues = function() {
+ return {
+ corge: "hi there"
+ }
+ }
+ expect(view.fieldValues()).to(equal, {
+ foo: "Foo",
+ bar: "Bar",
+ baz: false,
+ quux: '2',
+ textarea: "This too",
+ corge: "hi there"
+ });
+ });
+ });
+
describe("jQuery.fn.bindHtml(record, fieldName)", function() {
useExampleDomainModel();
@@ -42,7 +42,6 @@ Screw.Unit(function(c) { with(c) {
}
})
-
model = SampleModel.createFromRemote({
foo: "foo",
bar: "bar",
@@ -57,34 +56,6 @@ Screw.Unit(function(c) { with(c) {
});
});
- describe("#fieldValues", function() {
- it("returns a hash of name value pairs for all input elements on the view", function() {
- expect(view.fieldValues()).to(equal, {
- foo: "Foo",
- bar: "Bar",
- baz: false,
- textarea: "This too",
- quux: '2'
- });
- });
-
- it("if a customFieldValues method is present, merges its results into the returned fieldValues", function() {
- view.customFieldValues = function() {
- return {
- corge: "hi there"
- }
- }
- expect(view.fieldValues()).to(equal, {
- foo: "Foo",
- bar: "Bar",
- baz: false,
- quux: '2',
- textarea: "This too",
- corge: "hi there"
- });
- });
- });
-
describe("#fieldValuesDiffer()", function() {
it("returns true if any of the #fieldValues() don't match their corresponding field in model", function() {
view.model(model);

0 comments on commit 7583ee4

Please sign in to comment.