Created
February 6, 2014 19:35
-
-
Save bmulvihill/8851076 to your computer and use it in GitHub Desktop.
CoffeeScript to add serializeObject to JQuery, converts HTML form to JSON, modified from http://jsfiddle.net/davidhong/gP9bh/
This file contains 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
$.fn.serializeObject = -> | |
o = Object.create(null) | |
elementMapper = (element) -> | |
element.name = $.camelCase(element.name) | |
return element | |
appendToResult = (i, element) -> | |
node = o[element.name] | |
if ('undefined' != typeof node && node != null) | |
o[element.name] = if node.push then node.push(element.value) else [node, element.value] | |
else | |
o[element.name] = element.value | |
$.each($.map(@serializeArray(), elementMapper), appendToResult) | |
return o |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment