• serializeObject is not a core jquery function, see https://stackoverflow.com/questions/17488660/difference-between-serialize-and-serializeobject-jquery

    So, although not in core, serializeObject() is “known” in the jQuery datasphere, see https://plugins.jquery.com/serializeObject/. Since this is a “known” function it’s very dangerous to copy it under its original name, as other plugins may do the same – and there is no guarantee that a such-named function will in all cases work the same way.

    erp.js defines a function serializeObject(), the non-minified version of the function is found in assets/js/erp.js at line 621:

    jQuery.fn.serializeObject = function() {
        var o = {};
        var a = this.serializeArray();
        jQuery.each(a, function() {
            if (o[this.name] !== undefined) {
                if (!o[this.name].push) {
                    o[this.name] = [o[this.name]];
                }
                o[this.name].push(this.value || '');
            } else {
                o[this.name] = this.value || '';
            }
        });
        return o;
    };

    MailPoet 3 itself uses an exact copy of https://benalman.com/projects/jquery-misc-plugins/#serializeobject (minimised) at the end of assets/dist/js/mailpoet.651e3d8d.chunk.js:

    (function($,undefined){
    	'$:nomunge'; // Used by YUI compressor.
    	$.fn.serializeObject = function(){
    		var obj = {};
    		$.each( this.serializeArray(), function(i,o){
    			var n = o.name,
    		  v = o.value;
    		  obj[n] = obj[n] === undefined ? v
    		  : $.isArray( obj[n] ) ? obj[n].concat( v )
    		  : [ obj[n], v ];
    		});
    		return obj;
    	};
    })(jQuery);

    The above two versions are written in differing styles, but look as if they probably do the same (I’m a bit rusty in JS).

    Bounce Handler MailPoet uses an own function, in assets/js/mailpoet.js at line 18971 (but this is overridden by the same-named function from ERP):

    $.fn.serializeObject = function(coerce) {
    	var obj = {},
    	coerce_types = { 'true': !0, 'false': !1, 'null': null };
    
    	// Iterate over all name=value pairs.
    	$.each( this.serializeArray(), function(j,v){
    	var key = v.name,
    		val = v.value,
    		cur = obj,
    		i = 0,
    
    		// If key is more complex than 'foo', like 'a[]' or 'a[b][c]', split it
    		// into its component parts.
    		keys = key.split( '][' ),
    		keys_last = keys.length - 1;
    
    	// If the first keys part contains [ and the last ends with ], then []
    	// are correctly balanced.
    	if ( /\[/.test( keys[0] ) && /\]$/.test( keys[ keys_last ] ) ) {
    		// Remove the trailing ] from the last keys part.
    		keys[ keys_last ] = keys[ keys_last ].replace( /\]$/, '' );
    
    		// Split first keys part into two parts on the [ and add them back onto
    		// the beginning of the keys array.
    		keys = keys.shift().split('[').concat( keys );
    
    		keys_last = keys.length - 1;
    	} else {
    		// Basic 'foo' style key.
    		keys_last = 0;
    	}
    
    	// Coerce values.
    	if ( coerce ) {
    		val = val && !isNaN(val)            ? +val              // number
    		: val === 'undefined'             ? undefined         // undefined
    		: coerce_types[val] !== undefined ? coerce_types[val] // true, false, null
    		: val;                                                // string
    	}
    
    	if ( keys_last ) {
    		// Complex key, build deep object structure based on a few rules:
    		// * The 'cur' pointer starts at the object top-level.
    		// * [] = array push (n is set to array length), [n] = array if n is
    		//   numeric, otherwise object.
    		// * If at the last keys part, set the value.
    		// * For each keys part, if the current level is undefined create an
    		//   object or array based on the type of the next keys part.
    		// * Move the 'cur' pointer to the next level.
    		// * Rinse & repeat.
    		for ( ; i <= keys_last; i++ ) {
    		key = keys[i] === '' ? cur.length : keys[i];
    		cur = cur[key] = i < keys_last
    			? cur[key] || ( keys[i+1] && isNaN( keys[i+1] ) ? {} : [] )
    			: val;
    		}
    
    	} else {
    		// Simple key, even simpler rules, since only scalars and shallow
    		// arrays are allowed.
    
    		if ( $.isArray( obj[key] ) ) {
    		// val is already an array, so push on the next value.
    		obj[key].push( val );
    
    		} else if ( obj[key] !== undefined ) {
    		// val isn't an array, but since a second value has been specified,
    		// convert val into an array.
    		obj[key] = [ obj[key], val ];
    
    		} else {
    		// val is a scalar.
    		obj[key] = val;
    		}
    	}
    	});
    
    	return obj;
    };

    This is a much more complex function than the other two, and works quite differently. Since the datasphere seems to have more or less a consensus on what serializeObject should do, then any function which works differently really must be named differently – otherwise we get horrible collisions and something stops working. Like BHM’s save settings )-:

    Apart from that: Great plugin, and we’re really thankful for it!

    Tim Reeves

  • The topic ‘JS function name collision with WP ERP prevents saving settings’ is closed to new replies.