Skip to content

Loading…

"checkboxesAsBools" option #8

Merged
merged 1 commit into from

2 participants

@TaoK

Here is the "checkboxesAsBools" change with the change you requested - the "boolData" variable was a dirty leftover from my initial implementation (where I replaced the original functionality completely rather than making it optional), sorry about that.

Looks like github doesn't let me submit a pull request for just the "checkboxesAsBools" commit, as this commit is dependent on the previous formatting commit... Sorry if I'm making a mess of things or missing something here, but at least as separate commits the changes are more easily identified/understood.

@TaoK TaoK Add support for "checkboxesAsBools" option
Added support for "checkboxesAsBools" option, to be able to
distinguish between checkboxes that should really be false
(were specified as false in the serialized data) and checkboxes
that simply weren't present in the original serialized data.
This works with the serialize() modification at:
https://gist.github.com/1572512
210174c
@TaoK

ok, never mind that last comment - looks like the commit listing fixed itself after I actually completed the pull request.

@itsadok itsadok merged commit e2f2e50 into itsadok:master
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Jan 8, 2012
  1. @TaoK

    Add support for "checkboxesAsBools" option

    TaoK committed
    Added support for "checkboxesAsBools" option, to be able to
    distinguish between checkboxes that should really be false
    (were specified as false in the serialized data) and checkboxes
    that simply weren't present in the original serialized data.
    This works with the serialize() modification at:
    https://gist.github.com/1572512
This page is out of date. Refresh to see the latest.
Showing with 43 additions and 3 deletions.
  1. +3 −0 Readme.md
  2. +17 −3 src/jquery.deserialize.js
  3. +23 −0 test/index.html
View
3 Readme.md
@@ -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
View
20 src/jquery.deserialize.js
@@ -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;
@@ -34,6 +36,7 @@
return decodeURIComponent(d);
});
+ //collect data for checkbox handling
data[pair[0]] = pair[1];
var $input = $("[name='" + pair[0] + "']", this);
@@ -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);
View
23 test/index.html
@@ -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);
Something went wrong with that request. Please try again.