Skip to content

Commit

Permalink
handled nested array, fixed #45
Browse files Browse the repository at this point in the history
  • Loading branch information
layerssss committed Nov 27, 2014
1 parent 026541a commit 8d5742e
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 10 deletions.
2 changes: 1 addition & 1 deletion dist/jquery.serialize-object.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

23 changes: 14 additions & 9 deletions jquery.serialize-object.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,25 @@
}

function makeObject(root, value) {
var keys = root.match(patterns.key), pushed = false, k, k2 = '';

var keys = root.match(patterns.key), k;
function incrementPush(path, key) {
pushes[path] = pushes[path] || {};
if (pushes[path][key] === undefined) {
pushes[path][key] = 0;
}
if (pushed) {
return pushes[path][key];
}
pushed = true;
return pushes[path][key]++;
}

// nest, nest, ..., nest
while ((k = keys.pop()) !== undefined) {
// foo[]
if (patterns.push.test(k)) {
var idx = incrementPush(root.replace(/\[\]$/, ''));
var idx = incrementPush(keys.join('.'), k2);
value = build([], idx, value);
}

Expand All @@ -67,19 +78,13 @@
// foo; foo[bar]
else if (patterns.named.test(k)) {
value = build({}, k, value);
k2 = k;
}
}

return value;
}

function incrementPush(key) {
if (pushes[key] === undefined) {
pushes[key] = 0;
}
return pushes[key]++;
}

function encode(pair) {
switch ($('[name="' + pair.name + '"]', $form).attr("type")) {
case "checkbox":
Expand Down

0 comments on commit 8d5742e

Please sign in to comment.