Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save caramelopardalis/4d970f81aa20eaf1b7c96c9414a8bfdf to your computer and use it in GitHub Desktop.

Select an option

Save caramelopardalis/4d970f81aa20eaf1b7c96c9414a8bfdf to your computer and use it in GitHub Desktop.
jquery.dot-notation-param
/**
* 階層の異なるネストしたプロパティを jQuery によるシリアライズの形式で送信した場合、<br>
* Spring で Binding できない.<br>
* そのため、bracket notation ではなく、dot notation 形式でシリアライズする.
*/
(function(jQuery) {
// http://stackoverflow.com/questions/17351420/javascript-object-to-string
function transform(obj) {
var result = {};
recurse(obj, "");
return result;
function recurse(o, name) {
if (Object(o) !== o) {
result[name] = o;
} else if (Array.isArray(o)) {
for (var i = 0; i < o.length; i++) {
recurse(o[i], name + "[" + i + "]");
}
} else {
// if plain object?
for ( var p in o) {
recurse(o[p], name ? name + "." + p : p);
}
}
}
}
var _param = jQuery.param;
jQuery.param = function(obj) {
return _param(transform(obj));
};
})(jQuery);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment