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

"checkboxesAsBools" option #8

Merged
merged 1 commit into from Jan 8, 2012
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions Readme.md
Expand Up @@ -10,5 +10,8 @@ In contrary to some other plugins available it doesn't reinvent the wheel (by ut
// do not trigger change events
$("#form-id").deserialize(string, {noEvents: true});

// expect "true" and "false" values for checkboxes instead of presence/absence of field (see https://gist.github.com/1572512).
$("#form-id").deserialize(string, {checkboxesAsBools: true});

# Demo
For examples please see demo.html
20 changes: 17 additions & 3 deletions src/jquery.deserialize.js
Expand Up @@ -8,10 +8,12 @@
*
* do not trigger change events on elements
* $("form").deserialize(string, {noEvents: true});
*
* expect checkboxes to be serialized as boolean (true/false) rather than standard (present/missing)
* $("form").deserialize(string, {checkboxesAsBools: true});
**/
(function($) {
$.fn.deserialize = function(s, options) {

function optionallyTrigger(element,event) {
if (options.noEvents)
return;
Expand All @@ -34,6 +36,7 @@
return decodeURIComponent(d);
});

//collect data for checkbox handling
data[pair[0]] = pair[1];

var $input = $("[name='" + pair[0] + "']", this);
Expand All @@ -53,10 +56,21 @@
}
}

// checkboxes are not serialized -> missing means unchecked
$("input[type=checkbox]", this).each(function() {
var $input = $(this);
changeChecked($input, ($input.attr("name") in data));
if (options.checkboxesAsBools) {
//checkboxes are serialized as non-standard true/false, so only change value if provided (as explicit
// boolean) in the data. (so checkboxes behave like other fields - unspecified fields are unchanged)
if (data[pair[0]] == 'true')
changeChecked($input, true);
else if (data[pair[0]] == 'false')
changeChecked($input, false);
}
else {
//standard serialization, so checkboxes are not serialized -> ANY missing value means unchecked
// (no difference betwen "missing" and "false").
changeChecked($input, ($input.attr("name") in data));
}
});
};
})(jQuery);
23 changes: 23 additions & 0 deletions test/index.html
Expand Up @@ -89,6 +89,29 @@
});


module("checkboxesAsBools option");
test("checkbox is not changed on missing value, if checkboxesAsBools set", 1, function() {
$("#demo1").deserialize("user[is_admin]=");
$("#demo1").deserialize("othervalue=", {checkboxesAsBools: true});
equal($("#user_is_admin").is(":checked"), true, "user_is_admin (checkbox)");
});
test("checkbox is unset on false value, if checkboxesAsBools set", 1, function() {
$("#demo1").deserialize("user[is_admin]=");
$("#demo1").deserialize("user[is_admin]=false", {checkboxesAsBools: true});
equal($("#user_is_admin").is(":checked"), false, "user_is_admin (checkbox)");
});
test("checkbox is set on true value, if checkboxesAsBools set", 1, function() {
$("#demo1").deserialize("nothinghere");
$("#demo1").deserialize("user[is_admin]=true", {checkboxesAsBools: true});
equal($("#user_is_admin").is(":checked"), true, "user_is_admin (checkbox)");
});
test("checkbox is not changed on invalid value, if checkboxesAsBools set", 1, function() {
$("#demo1").deserialize("user[is_admin]=");
$("#demo1").deserialize("user[is_admin]=nonsense", {checkboxesAsBools: true});
equal($("#user_is_admin").is(":checked"), true, "user_is_admin (checkbox)");
});


module("All in one go");
test("All in one go", 7, function() {
$("#demo1").deserialize(test_input);
Expand Down