Make WordPress Core

Changeset 20257


Ignore:
Timestamp:
03/22/2012 06:52:44 AM (15 years ago)
Author:
koopersmith
Message:

Theme Customizer: In JavaScript, add the ability to execute a callback once a set of values have been initialized. see #19910.

File:
1 edited

Legend:

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

    r20128 r20257  
    378378                initialize: function( options ) {
    379379                        api.Value.prototype.initialize.call( this, {}, options || {} );
     380                        this._deferreds = {};
    380381                },
    381382
    382383                instance: function( id ) {
    383                         return this.value( id );
     384                        if ( arguments.length === 1 )
     385                                return this.value( id );
     386
     387                        return this.when.apply( this, arguments );
    384388                },
    385389
    … …  
    398402                        this._value[ id ] = value;
    399403                        this._value[ id ]._parent = this._value;
     404
     405                        if ( this._deferreds[ id ] )
     406                                this._deferreds[ id ].resolve();
     407
    400408                        return this._value[ id ];
    401409                },
    … …  
    410418                remove: function( id ) {
    411419                        delete this._value[ id ];
     420                        delete this._deferreds[ id ];
    412421                },
    413422
    … …  
    423432                        value = this.value( id );
    424433                        return value[ fn ].apply( value, args );
     434                },
     435
     436                /**
     437                 * Runs a callback once all requested values exist.
     438                 *
     439                 * when( ids*, callback );
     440                 *
     441                 * For example:
     442                 *     when( id1, id2, id3, function( value1, value2, value3 ) {} );
     443                 */
     444                when: function() {
     445                        var self = this,
     446                                ids = slice.call( arguments ),
     447                                callback = ids.pop();
     448
     449                        $.when.apply( $, $.map( ids, function( id ) {
     450                                if ( self.has( id ) )
     451                                        return;
     452
     453                                return self._deferreds[ id ] || ( self._deferreds[ id ] = $.Deferred() );
     454                        })).done( function() {
     455                                var values = $.map( ids, function( id ) {
     456                                                return self( id );
     457                                        });
     458
     459                                // If a value is missing, we've used at least one expired deferred.
     460                                // Call Values.when again to update our master deferred.
     461                                if ( values.length !== ids.length ) {
     462                                        ids.push( callback );
     463                                        self.when.apply( self, ids );
     464                                        return;
     465                                }
     466
     467                                callback.apply( self, values );
     468                        });
    425469                }
    426470        });
Note: See TracChangeset for help on using the changeset viewer.