Skip to content

Commit

Permalink
Remove camel casing of keys as suggested by issue #2
Browse files Browse the repository at this point in the history
This is consistent with other $ methods such as:
	- $.serializeArray
	- $.serialize

There is no reason why it should follow $.data conventions. Although
that isn't hard to achieve.
  • Loading branch information
hongymagic committed Jan 23, 2013
1 parent efac2b9 commit 7789084
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 35 deletions.
47 changes: 33 additions & 14 deletions README.markdown
Expand Up @@ -13,28 +13,47 @@ JSON much easier to work with than DOM or string manipulation.

# How do I use it?

If you want to see the code and demo first: http://jsfiddle.net/davidhong/gP9bh/
If you want to see the code and demo first: http://jsfiddle.net/davidhong/PRpJT/

Simple include the `jQuery.serializeObject.js` along with any `jQuery` instance
Simply include the `jQuery.serializeObject.js` along with any `jQuery` instance
and use it like `$.serialize`.

If you have a `form` like the following:

<form id="minutes">
<input type="text" name="subject" />
<input type="text" name="minute-taker" />
<input type="text" name="attendees" />
...
</form>
<form id="minutes">
<input type="text" name="subject">
<input type="text" name="minute-taker">
<!-- ... -->
<input type="checkbox" name="attendees" value="David" checked="checked">
<input type="checkbox" name="attendees" value="Daniel" checked="checked">
<input type="checkbox" name="attendees" value="Darwin" checked="checked">
</form>

and wish to convert them to a JSON object:

var minutes = $('form#minutes').serializeObject();
var minutes = $('form#minutes').serializeObject();

will return:

{
subject: '',
minuteTaker: '',
attendees: ''
}
{
"subject": "",
"minute-taker": "",
"attendees": [
"David",
"Daniel",
"Darwin"
]
}

## Change log

### 2.0.0

*Major version change: Camel casing of names have been removed. Please use
version 1.0.4 if you require camel casing of names.*

- Remove `$.data` like camelCasing on names

### 1.0.4

- Fix an issue (#2) where arrays longer than 2 resulted in incorrect values
2 changes: 1 addition & 1 deletion dist/jquery.serializeObject.min.js

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

28 changes: 10 additions & 18 deletions jquery.serializeObject.js
@@ -1,28 +1,20 @@
//
// Use internal $.serializeArray to get list of form elements which is consistent with $.serialize
// Use internal $.serializeArray to get list of form elements which is
// consistent with $.serialize
//
// And to avoid names such as
// => object["favorite-color"]
// From version 2.0.0, $.serializeObject will stop converting [name] values
// to camelCase format. This is *consistent* with other serialize methods:
//
// We camelcase the name part, so the notation becomes
// => object["favoriteColor"]
// - $.serialize
// - $.serializeArray
//
// Conveniently, this allows period notation to be used.
// => object.favoriteColor
//
// This behaviour is similar to $(element).data()
//
// $('<div data-favorite-color="yellow"></div>').data()
// => { favoriteColor: 'yellow' }
// If you require camel casing, you can either download version 1.0.4 or map
// them yourself.
//
$.fn.serializeObject = function () {
"use strict";

var result = Object.create(null);
var mapper = function (element) {
element.name = $.camelCase(element.name);
return element;
};
var result = {};
var extend = function (i, element) {
var node = result[element.name];

Expand All @@ -43,6 +35,6 @@ $.fn.serializeObject = function () {
// For each serialzable element, convert element names to camelCasing and
// extend each of them to a JSON object

$.each($.map(this.serializeArray(), mapper), extend);
$.each(this.serializeArray(), extend);
return result;
};
2 changes: 1 addition & 1 deletion serializeObject.jquery.json
@@ -1,6 +1,6 @@
{
"name": "serializeObject",
"version": "1.0.4",
"version": "2.0.0",
"title": "jQuery serializeObject",
"author": {
"name": "David G. Hong",
Expand Down
2 changes: 1 addition & 1 deletion test/serialization-test.js
Expand Up @@ -9,7 +9,7 @@ test('simple form test', function () {
age: '21',
email: 'john.apple@apple.com',
password: '',
legalAge: 'yes'
'legal-age': 'yes'
};

deepEqual(data, expected, 'Key/value pairs should be identical');
Expand Down

0 comments on commit 7789084

Please sign in to comment.