Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Form tag no longer required in view and disabled fields are ignored #18

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -480,7 +480,6 @@ For more information on Key Assignment Validators, see the full
There some known limitations in Backbone.Syphon, partially by design and
partially implemented as default behaivors.

* You must have a `<form>` within your view's `$el`
* An input of type `checkbox` will return a boolean value. This can be
overriden by replacing the Input Reader for checkboxes.

Expand Down
24 changes: 24 additions & 0 deletions spec/javascripts/deserialize.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -214,4 +214,28 @@ describe("deserializing an object into a form", function(){
});
});


describe("when deserializing without a form", function(){
var View = Backbone.View.extend({
render: function(){
this.$el.html("<input type='text' name='foo'>");
}
});

var view;

beforeEach(function(){
view = new View();
view.render();

Backbone.Syphon.deserialize(view, { foo: "bar" });
});

it("should set the input's value", function(){
var result = view.$('input[name=foo]').val();
expect(result).toBe("bar");
});
});


});
71 changes: 71 additions & 0 deletions spec/javascripts/serialize.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,58 @@ describe("serializing a form", function(){
});
});

describe("when serializing forms with disabled text fields", function(){
var View = Backbone.View.extend({
render: function(){
this.$el.html("<form><input type='text' name='foo' value='bar'><input type='text' name='ignore' value='bar' disabled></form>");
}
});

var view, result;

beforeEach(function(){
view = new View();
view.render();

result = Backbone.Syphon.serialize(view);
});

it("should return an object with a key from the text input name, and ignore disabled", function(){
expect(result.hasOwnProperty("foo")).toBe(true)
expect(result.hasOwnProperty("ignore")).toBe(false)
});

it("should have the input's value", function(){
expect(result.foo).toBe("bar");
});
});

describe("when serializing forms with disabled select field", function(){
var View = Backbone.View.extend({
render: function(){
this.$el.html("<form><input type='text' name='foo' value='bar'><select name='ignore' disabled='disabled'><option selected='selected' value='bar'>bar</option><option value='bar2'>bar2</option></select></form>");
}
});

var view, result;

beforeEach(function(){
view = new View();
view.render();

result = Backbone.Syphon.serialize(view);
});

it("should return an object with a key from the text input name, and ignore disabled", function(){
expect(result.hasOwnProperty("foo")).toBe(true)
expect(result.hasOwnProperty("ignore")).toBe(false)
});

it("should have the input's value", function(){
expect(result.foo).toBe("bar");
});
});

describe("when the view is actually a form", function() {
var View = Backbone.View.extend({
tagName: "form",
Expand Down Expand Up @@ -248,4 +300,23 @@ describe("serializing a form", function(){
});
});

describe("when the view does not contain a form", function() {
var View = Backbone.View.extend({
render: function(){
this.$el.html("<input type='text' name='foo' value='bar'>");
}
});

beforeEach(function() {
view = new View();
view.render();

result = Backbone.Syphon.serialize(view);
});

it("retrieves the inputs' values", function() {
expect(result.foo).toBe("bar");
});
});

});
14 changes: 12 additions & 2 deletions src/backbone.syphon.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,14 +89,24 @@ Backbone.Syphon = (function(Backbone, $, _){
// from the form
var getInputElements = function(view, config){
var form = getForm(view);
var elements = form.elements;
var elements;

if (form) {
elements = form.elements;
} else {
// fallback on :input selector
elements = view.$(':input');
}

elements = _.reject(elements, function(el){
// Reject disabled fields always
if (el.hasAttribute('disabled')) { return true; }
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be excluded from this PR.

Cross ref #70


var reject;
var type = getElementType(el);
var extractor = config.keyExtractors.get(type);
var identifier = extractor($(el));

var foundInIgnored = _.include(config.ignoredTypes, type);
var foundInInclude = _.include(config.include, identifier);
var foundInExclude = _.include(config.exclude, identifier);
Expand Down