From ba845e1f84d10dbe74d37fca1a1f694d98dcf821 Mon Sep 17 00:00:00 2001 From: Ralph Holzmann Date: Tue, 15 Nov 2011 15:32:16 -0600 Subject: [PATCH 1/3] Added the option to pass an object map to DOM formParams to populate a form. In other words, formParams is now a getter and a setter. --- dom/form_params/form_params.js | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/dom/form_params/form_params.js b/dom/form_params/form_params.js index 2644fe24..cc1268f0 100644 --- a/dom/form_params/form_params.js +++ b/dom/form_params/form_params.js @@ -49,13 +49,25 @@ steal("jquery/dom").then(function( $ ) { * empty string should not be added to the result. Defaults to false. * @return {Object} An object of name-value pairs. */ - formParams: function( convert ) { - if ( this[0].nodeName.toLowerCase() == 'form' && this[0].elements ) { + formParams: function( params, convert ) { + if ( !! params === params ) { + convert = params; + params = null; + } + + if ( params ) { + return this.setParams( params ); + } else if ( this[0].nodeName.toLowerCase() == 'form' && this[0].elements ) { return jQuery(jQuery.makeArray(this[0].elements)).getParams(convert); } return jQuery("input[name], textarea[name], select[name]", this[0]).getParams(convert); }, + setParams: function( params ) { + this.find("[name]").val(function(){ + return params[ $(this).attr("name") ] || ''; + }); + }, getParams: function( convert ) { var data = {}, current; From b43e8f439e8cb7e615cc467ffc217aacc7a90478 Mon Sep 17 00:00:00 2001 From: Ralph Holzmann Date: Tue, 15 Nov 2011 15:39:09 -0600 Subject: [PATCH 2/3] Bug fix. --- dom/form_params/form_params.js | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/dom/form_params/form_params.js b/dom/form_params/form_params.js index cc1268f0..96e8409a 100644 --- a/dom/form_params/form_params.js +++ b/dom/form_params/form_params.js @@ -65,7 +65,10 @@ steal("jquery/dom").then(function( $ ) { }, setParams: function( params ) { this.find("[name]").val(function(){ - return params[ $(this).attr("name") ] || ''; + var value = params[ $(this).attr("name") ]; + if ( value ) { + return value; + } }); }, getParams: function( convert ) { From 0404ba86c31810882539d4aed844ae69b96626e2 Mon Sep 17 00:00:00 2001 From: Ralph Holzmann Date: Wed, 16 Nov 2011 00:08:55 -0600 Subject: [PATCH 3/3] Added fixes for formParams setter. Now handles radio and checkboxes. --- dom/form_params/form_params.js | 39 ++++++++++++++++++++++++++++------ 1 file changed, 33 insertions(+), 6 deletions(-) diff --git a/dom/form_params/form_params.js b/dom/form_params/form_params.js index 96e8409a..cbfc7f6c 100644 --- a/dom/form_params/form_params.js +++ b/dom/form_params/form_params.js @@ -45,12 +45,17 @@ steal("jquery/dom").then(function( $ ) { * * @demo jquery/dom/form_params/form_params.html * - * @param {Boolean} [convert=false] True if strings that look like numbers and booleans should be converted and if - * empty string should not be added to the result. Defaults to false. + * @param {Object} [params] If an object is passed, the form will be repopulated + * with the values of the object based on the name of the inputs within + * the form + * @param {Boolean} [convert=false] True if strings that look like numbers + * and booleans should be converted and if empty string should not be added + * to the result. Defaults to false. * @return {Object} An object of name-value pairs. */ formParams: function( params, convert ) { + // Quick way to determine if something is a boolean if ( !! params === params ) { convert = params; params = null; @@ -64,12 +69,34 @@ steal("jquery/dom").then(function( $ ) { return jQuery("input[name], textarea[name], select[name]", this[0]).getParams(convert); }, setParams: function( params ) { - this.find("[name]").val(function(){ - var value = params[ $(this).attr("name") ]; + + // Find all the inputs + this.find("[name]").each(function() { + + var value = params[ $(this).attr("name") ], + $this; + + // Don't do all this work if there's no value if ( value ) { - return value; + $this = $(this); + + // Nested these if statements for performance + if ( $this.is(":radio") ) { + if ( $this.val() == value ) { + $this.attr("checked", true); + } + } else if ( $this.is(":checkbox") ) { + // Convert single value to an array to reduce + // complexity + value = $.isArray( value ) ? value : [value]; + if ( $.inArray( $this.val(), value ) > -1) { + $this.attr("checked", true); + } + } else { + $this.val( value ); + } } - }); + }); }, getParams: function( convert ) { var data = {},