Skip to content

Instantly share code, notes, and snippets.

@bmulvihill
Created February 6, 2014 19:35
Show Gist options
  • Save bmulvihill/8851076 to your computer and use it in GitHub Desktop.
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/
$.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