From 7789084425b353624fc95487024459f364d11df6 Mon Sep 17 00:00:00 2001 From: hongymagic Date: Wed, 23 Jan 2013 13:38:23 +1100 Subject: [PATCH] Remove camel casing of keys as suggested by issue #2 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. --- README.markdown | 47 +++++++++++++++++++++--------- dist/jquery.serializeObject.min.js | 2 +- jquery.serializeObject.js | 28 +++++++----------- serializeObject.jquery.json | 2 +- test/serialization-test.js | 2 +- 5 files changed, 46 insertions(+), 35 deletions(-) diff --git a/README.markdown b/README.markdown index fcb315d..bb3751b 100644 --- a/README.markdown +++ b/README.markdown @@ -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: -
- - - - ... -
+
+ + + + + + +
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 diff --git a/dist/jquery.serializeObject.min.js b/dist/jquery.serializeObject.min.js index 9492def..8aac8a4 100644 --- a/dist/jquery.serializeObject.min.js +++ b/dist/jquery.serializeObject.min.js @@ -1 +1 @@ -$.fn.serializeObject=function(){"use strict";var a=Object.create(null),b=function(a){return a.name=$.camelCase(a.name),a},c=function(b,c){var d=a[c.name];"undefined"!=typeof d&&d!==null?$.isArray(d)?d.push(c.value):a[c.name]=[d,c.value]:a[c.name]=c.value};return $.each($.map(this.serializeArray(),b),c),a}; \ No newline at end of file +$.fn.serializeObject=function(){"use strict";var a={},b=function(b,c){var d=a[c.name];"undefined"!=typeof d&&d!==null?$.isArray(d)?d.push(c.value):a[c.name]=[d,c.value]:a[c.name]=c.value};return $.each(this.serializeArray(),b),a}; \ No newline at end of file diff --git a/jquery.serializeObject.js b/jquery.serializeObject.js index 7859ade..1c8dac3 100644 --- a/jquery.serializeObject.js +++ b/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() -// -// $('
').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]; @@ -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; }; diff --git a/serializeObject.jquery.json b/serializeObject.jquery.json index 5f5b1dd..32d4cae 100644 --- a/serializeObject.jquery.json +++ b/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", diff --git a/test/serialization-test.js b/test/serialization-test.js index 8c61f3a..8f43b66 100644 --- a/test/serialization-test.js +++ b/test/serialization-test.js @@ -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');