Make WordPress Core

Changeset 26548


Ignore:
Timestamp:
12/03/2013 12:07:49 AM (13 years ago)
Author:
ocean90
Message:

Customizer: Don't trigger a change event if two unchanged object values are equal.

In JavaScript: {"title":"Foo"} !== {"title":"Foo"}
Underscore.js provides an isEqual function to compare two values to see if they have the same contents, which can be use instead.
Because only one function of Underscore.js is needed we just add a copy of _.isEqual to customize-base.js.

props westonruter.
fixes #26061.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/js/customize-base.js

    r26542 r26548  
    177177
    178178                        // Bail if the sanitized value is null or unchanged.
    179                         if ( null === to || this._value === to )
     179                        if ( null === to || this.isEqual( to ) )
    180180                                return this;
    181181
    … …  
    252252                        });
    253253                        return this;
    254                 }
     254                },
     255
     256                isEqual: function( to ) {
     257                        return this._eq( this._value, to, [], [] );
     258                },
     259
     260                /**
     261                 * Internal recursive comparison function for `isEqual`.
     262                 * Copied from Underscore.js.
     263                 */
     264                /* jshint ignore:start */
     265                _eq: function(a, b, aStack, bStack) {
     266                        if (a === b) return a !== 0 || 1 / a == 1 / b;
     267                        if (a == null || b == null) return a === b;
     268
     269                        var className = toString.call(a);
     270                        if (className != toString.call(b)) return false;
     271                        switch (className) {
     272                                case '[object String]':
     273                                        return a == String(b);
     274                                case '[object Number]':
     275                                        return a != +a ? b != +b : (a == 0 ? 1 / a == 1 / b : a == +b);
     276                                case '[object Date]':
     277                                case '[object Boolean]':
     278                                        return +a == +b;
     279                                case '[object RegExp]':
     280                                        return a.source == b.source &&
     281                                                                a.global == b.global &&
     282                                                                a.multiline == b.multiline &&
     283                                                                a.ignoreCase == b.ignoreCase;
     284                        }
     285                        if (typeof a != 'object' || typeof b != 'object') return false;
     286                        var length = aStack.length;
     287                        while (length--) {
     288                                if (aStack[length] == a) return bStack[length] == b;
     289                        }
     290                        var aCtor = a.constructor, bCtor = b.constructor;
     291                        if (aCtor !== bCtor && !((typeof aCtor === 'function') && (aCtor instanceof aCtor) &&
     292                                                                        (typeof bCtor === 'function') && (bCtor instanceof bCtor))) {
     293                                return false;
     294                        }
     295                        aStack.push(a);
     296                        bStack.push(b);
     297                        var size = 0, result = true;
     298
     299                        if (className == '[object Array]') {
     300                                size = a.length;
     301                                result = size == b.length;
     302                                if (result) {
     303                                        while (size--) {
     304                                                if (!(result = this._eq(a[size], b[size], aStack, bStack))) break;
     305                                        }
     306                                }
     307                        } else {
     308                                for (var key in a) {
     309                                        if (hasOwnProperty.call(a, key)) {
     310                                                size++;
     311                                                if (!(result = hasOwnProperty.call(b, key) && this._eq(a[key], b[key], aStack, bStack))) break;
     312                                        }
     313                                }
     314                                if (result) {
     315                                        for (key in b) {
     316                                                if (hasOwnProperty.call(b, key) && !(size--)) break;
     317                                        }
     318                                        result = !size;
     319                                }
     320                        }
     321                        aStack.pop();
     322                        bStack.pop();
     323                        return result;
     324                }
     325                /* jshint ignore:end */
    255326        });
    256327
Note: See TracChangeset for help on using the changeset viewer.