Permalink
Browse files

Support input elements on the original collection, as well as multipl…

…e elements to search inside of. Fixes issue #7
  • Loading branch information...
1 parent 2f03335 commit 21a9d4fa644f85c4c6904209a451cc61edcde63d @danheberden committed Nov 12, 2012
Showing with 60 additions and 20 deletions.
  1. +19 −9 dist/serializeObject.js
  2. +2 −2 dist/serializeObject.min.js
  3. +18 −8 src/serializeObject.js
  4. +6 −0 test/serializeObject.html
  5. +15 −1 test/serializeObject_test.js
@@ -1,42 +1,52 @@
-/*! serializeObject - v1.0.0 - 2012-08-04
+/*! serializeObject - v1.0.0 - 2012-11-12
* https://github.com/danheberden/serializeObject
* Copyright (c) 2012 Dan Heberden; Licensed MIT, GPL */
(function( $ ){
$.fn.serializeObject = function() {
+
+ // don't do anything if we didn't get any elements
if ( this.length < 1) {
return false;
}
- var $el = this;
var data = {};
var lookup = data; //current reference of data
+ var selector = ':input[type!="checkbox"][type!="radio"], input:checked';
+ var parse = function() {
- $el.find( ':input[type!="checkbox"][type!="radio"], input:checked' ).each( function() {
// data[a][b] becomes [ data, a, b ]
var named = this.name.replace(/\[([^\]]+)?\]/g, ',$1').split(',');
var cap = named.length - 1;
+ var $el = $( this );
// Ensure that only elements with valid `name` properties will be serialized
if ( named[ 0 ] ) {
for ( var i = 0; i < cap; i++ ) {
- // move down the tree - create objects or array if necessary
- lookup = lookup[ named[i] ] = lookup[ named[i] ] ||
- ( named[ i + 1 ] === "" ? [] : {} );
+ // move down the tree - create objects or array if necessary
+ lookup = lookup[ named[i] ] = lookup[ named[i] ] ||
+ ( named[ i + 1 ] === "" ? [] : {} );
}
// at the end, push or assign the value
if ( lookup.length !== undefined ) {
- lookup.push( $( this ).val() );
+ lookup.push( $el.val() );
}else {
- lookup[ named[ cap ] ] = $( this ).val();
+ lookup[ named[ cap ] ] = $el.val();
}
// assign the reference back to root
lookup = data;
}
- });
+ };
+
+ // first, check for elements passed into this function
+ this.filter( selector ).each( parse );
+
+ // then parse possible child elements
+ this.find( selector ).each( parse );
+ // return data
return data;
};
}( jQuery ));
@@ -1,4 +1,4 @@
-/*! serializeObject - v1.0.0 - 2012-08-04
+/*! serializeObject - v1.0.0 - 2012-11-12
* https://github.com/danheberden/serializeObject
* Copyright (c) 2012 Dan Heberden; Licensed MIT, GPL */
-(function(a){a.fn.serializeObject=function(){if(this.length<1)return!1;var b=this,c={},d=c;return b.find(':input[type!="checkbox"][type!="radio"], input:checked').each(function(){var b=this.name.replace(/\[([^\]]+)?\]/g,",$1").split(","),e=b.length-1;if(b[0]){for(var f=0;f<e;f++)d=d[b[f]]=d[b[f]]||(b[f+1]===""?[]:{});d.length!==undefined?d.push(a(this).val()):d[b[e]]=a(this).val(),d=c}}),c}})(jQuery);
+(function(e){e.fn.serializeObject=function(){if(this.length<1)return!1;var t={},n=t,r=':input[type!="checkbox"][type!="radio"], input:checked',i=function(){var r=this.name.replace(/\[([^\]]+)?\]/g,",$1").split(","),i=r.length-1,s=e(this);if(r[0]){for(var o=0;o<i;o++)n=n[r[o]]=n[r[o]]||(r[o+1]===""?[]:{});n.length!==undefined?n.push(s.val()):n[r[i]]=s.val(),n=t}};return this.filter(r).each(i),this.find(r).each(i),t}})(jQuery);
@@ -7,39 +7,49 @@
*/
(function( $ ){
$.fn.serializeObject = function() {
+
+ // don't do anything if we didn't get any elements
if ( this.length < 1) {
return false;
}
- var $el = this;
var data = {};
var lookup = data; //current reference of data
+ var selector = ':input[type!="checkbox"][type!="radio"], input:checked';
+ var parse = function() {
- $el.find( ':input[type!="checkbox"][type!="radio"], input:checked' ).each( function() {
// data[a][b] becomes [ data, a, b ]
var named = this.name.replace(/\[([^\]]+)?\]/g, ',$1').split(',');
var cap = named.length - 1;
+ var $el = $( this );
// Ensure that only elements with valid `name` properties will be serialized
if ( named[ 0 ] ) {
for ( var i = 0; i < cap; i++ ) {
- // move down the tree - create objects or array if necessary
- lookup = lookup[ named[i] ] = lookup[ named[i] ] ||
- ( named[ i + 1 ] === "" ? [] : {} );
+ // move down the tree - create objects or array if necessary
+ lookup = lookup[ named[i] ] = lookup[ named[i] ] ||
+ ( named[ i + 1 ] === "" ? [] : {} );
}
// at the end, push or assign the value
if ( lookup.length !== undefined ) {
- lookup.push( $( this ).val() );
+ lookup.push( $el.val() );
}else {
- lookup[ named[ cap ] ] = $( this ).val();
+ lookup[ named[ cap ] ] = $el.val();
}
// assign the reference back to root
lookup = data;
}
- });
+ };
+
+ // first, check for elements passed into this function
+ this.filter( selector ).each( parse );
+
+ // then parse possible child elements
+ this.find( selector ).each( parse );
+ // return data
return data;
};
}( jQuery ));
@@ -38,6 +38,12 @@ <h2 id="qunit-userAgent"></h2>
<option value="opt"></option>
</select>
</form>
+ <div id="test-form-3">
+ <input name="text1" value="txt-one" />
+ <input type="checkbox" name="top[child][]" value="1" checked="checked" />
+ <input type="checkbox" name="top[child][]" value="2" checked="checked" />
+ <input type="checkbox" name="top[child][]" value="3" checked="checked" />
+ </div>
</div>
</body>
</html>
@@ -55,9 +55,23 @@
}
};
- deepEqual( result, looksLike, "jQuery.fn.serizlieObject() correctly generates form object" );
+ deepEqual( result, looksLike, "jQuery.fn.serializeObject() correctly generates form object" );
});
+ test( "parses the entire collection", function() {
+ expect(1);
+
+ var result = $( '#test-form-3' ).find('input').serializeObject();
+ var looksLike = { text1: "txt-one",
+ top: {
+ child: [ "1", "2", "3" ]
+ }
+ };
+
+ deepEqual( result, looksLike, "jQuery.fn.serizlieObject() correctly generates object based on elements in the jQuery collection" );
+ });
+
+
}(jQuery));

0 comments on commit 21a9d4f

Please sign in to comment.