Wednesday, November 17, 2010

jQuery extend() removes undefined values...

If you pass in an undefined value into the data for the $.ajax call(), this undefined field will be removed during the $.ajax() call when extending the variables:

var params = { width:1680, height:1050, picture: undefined };

$.ajax({url: 'www.myurl.com', data: params});


This will invoke the $.ajax function, which then invokes the extend() function:
   ajax: function( origSettings ) {
        var s = jQuery.extend(true, {}, jQuery.ajaxSettings, origSettings);

The extend() function tries to remove out the undefined/null values:
jQuery.extend = jQuery.fn.extend = function() {
       // Only deal with non-null/undefined values                                                                                                                                                                                          
        if ( (options = arguments[ i ]) != null ) {
            // Extend the base object                                                                                                                                                                                                        
          
                // Don't bring in undefined values                                                                                                                                                                                           
                } else if ( copy !== undefined ) {
                    target[ name ] = copy;

No comments:

Post a Comment