Permalink
Browse files

Merge pull request #44 from gf3/serialize-checkboxes

Fix multiple checked checkbox serialization bug
  • Loading branch information...
Ryan Eastridge
Ryan Eastridge committed Jun 20, 2012
2 parents da63c43 + 84a2eb5 commit 8a0321055056841e703bac20e6c20d502bc24e05
Showing with 10 additions and 2 deletions.
  1. +3 −1 test/test.js
  2. +7 −1 thorax.js
View
@@ -13,7 +13,7 @@ $(function() {
'letter-multiple-item.handlebars': Handlebars.compile("<li>{{letter}}</li><li>{{letter}}</li>"),
'parent.handlebars': Handlebars.compile("<div>{{view child}}</div>"),
'child.handlebars': Handlebars.compile("<div>{{value}}</div>"),
- 'form.handlebars': Handlebars.compile('<form><input name="one"/><select name="two"><option value="a">a</option><option value="b">b</option></select><input name="three[four]"/></form>')
+ 'form.handlebars': Handlebars.compile('<form><input name="one"/><select name="two"><option value="a">a</option><option value="b">b</option></select><input name="three[four]"/><input name="five" value="A" type="checkbox" /><input name="five" value="B" type="checkbox" checked /><input name="five" value="C" type="checkbox" checked /><input name="six" value="LOL" type="checkbox" checked /></form>')
};
var LetterModel = Thorax.Model.extend({});
@@ -515,6 +515,8 @@ $(function() {
view.render();
var attributes = view.serialize();
equal(attributes.one, "", 'serialize empty attributes');
+ deepEqual(attributes.five, ['B', 'C'], 'serialize empty attributes');
+ equal(attributes.six, 'LOL', 'serialize empty attributes');
view.setModel(model);
attributes = view.serialize();
equal(attributes.one, 'a', 'serialize attributes from model');
View
@@ -557,7 +557,13 @@
var value = getInputValue.call(this);
if (typeof value !== 'undefined') {
objectAndKeyFromAttributesAndName(attributes, this.name, {mode: 'serialize'}, function(object, key) {
- object[key] = value;
+ if (!object[key]) {
+ object[key] = value;
+ } else if (_.isArray(object[key])) {
+ object[key].push(value);
+ } else {
+ object[key] = [object[key], value];
+ }
});
}
});

0 comments on commit 8a03210

Please sign in to comment.