Bug Tracker

Modify

Ticket #12105 (closed enhancement: duplicate)

Opened 10 months ago

Last modified 10 months ago

jQuery.extend - extend model (custom object) by plain object (JSON response from server)

Reported by: sergey.buturlakin@… Owned by:
Priority: undecided Milestone: None
Component: unfiled Version: git
Keywords: Cc:
Blocking: Blocked by:

Description

Now jQuery.extend (deep copy) doesn't allow to extend custom object by server response (plain object from JSON)

 http://jsfiddle.net/C4duF/

On deep copy jQuery requires both property values to be plain objects to proceed recursively, otherwise it replaces value in target.

Could this be changed to requirement when target could be any object (not only plain) but other side must be plain?

// Recurse if we're merging plain objects or arrays
if ( deep && copy && ( jQuery.isPlainObject(copy) || (copyIsArray = jQuery.isArray(copy)) ) ) {
	if ( copyIsArray ) {
		copyIsArray = false;
		clone = src && jQuery.isArray(src) ? src : [];

	} else {
		// CHNAGE FROM:
		//clone = src && jQuery.isPlainObject(src) ? src : {};
		// CHNAGE TO:
		clone = !src || jQuery.type(src) !== "object" || src.nodeType || jQuery.isWindow( src ) ? {} : src;
	}

	// Never move original objects, clone them
	target[ name ] = jQuery.extend( deep, clone, copy );

// Don't bring in undefined values
} else if ( copy !== undefined ) {
	target[ name ] = copy;
}

Change History

comment:1 Changed 10 months ago by dmethvin

  • Status changed from new to closed
  • Resolution set to duplicate

comment:2 Changed 10 months ago by dmethvin

Duplicate of #10014.

Please follow the  bug reporting guidlines and use  jsFiddle when providing test cases and demonstrations instead of pasting the code in the ticket.

View

Add a comment

Modify Ticket

Action
as closed
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.