Created
October 21, 2016 08:58
-
-
Save caramelopardalis/4d970f81aa20eaf1b7c96c9414a8bfdf to your computer and use it in GitHub Desktop.
jquery.dot-notation-param
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * 階層の異なるネストしたプロパティを 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