Make WordPress Core

Changeset 20795


Ignore:
Timestamp:
05/15/2012 09:28:21 PM (15 years ago)
Author:
koopersmith
Message:

Theme Customizer: Add an Events mixin to wp.customize, used by default in wp.customize.Messenger. see #19910.

File:
1 edited

Legend:

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

    r20745 r20795  
    121121
    122122        /* =====================================================================
    123          * Light two-way binding.
     123         * Events mixin.
     124         * ===================================================================== */
     125
     126        api.Events = {
     127                trigger: function( id ) {
     128                        if ( this.topics && this.topics[ id ] )
     129                                this.topics[ id ].fireWith( this, slice.call( arguments, 1 ) );
     130                },
     131
     132                bind: function( id, callback ) {
     133                        this.topics = this.topics || {};
     134                        this.topics[ id ] = this.topics[ id ] || $.Callbacks();
     135                        this.topics[ id ].add.apply( this.topics[ id ], slice.call( arguments, 1 ) );
     136                },
     137
     138                unbind: function( id, callback ) {
     139                        if ( this.topics && this.topics[ id ] )
     140                                this.topics[ id ].remove.apply( this.topics[ id ], slice.call( arguments, 1 ) );
     141                }
     142        };
     143
     144        /* =====================================================================
     145         * Observable values that support two-way binding.
    124146         * ===================================================================== */
    125147
    … …  
    227249                }
    228250        });
     251
     252        /* =====================================================================
     253         * A collection of observable values.
     254         * ===================================================================== */
    229255
    230256        api.Values = api.Class.extend({
    … …  
    345371        });
    346372
     373
     374        /* =====================================================================
     375         * An observable value that syncs with an element.
     376         *
     377         * Handles inputs, selects, and textareas by default.
     378         * ===================================================================== */
     379
    347380        api.ensure = function( element ) {
    348381                return typeof element == 'string' ? $( element ) : element;
    … …  
    449482                        });
    450483
    451                         this.topics = {};
    452 
    453484                        this.receive = $.proxy( this.receive, this );
    454485                        $( window ).on( 'message', this.receive );
    … …  
    470501                        message = JSON.parse( event.data );
    471502
    472                         if ( message && message.id && typeof message.data !== 'undefined' && this.topics[ message.id ] )
    473                                 this.topics[ message.id ].fireWith( this, [ message.data ]);
     503                        if ( message && message.id && typeof message.data !== 'undefined' )
     504                                this.trigger( message.id, message.data );
    474505                },
    475506
    … …  
    484515                        message = JSON.stringify({ id: id, data: data });
    485516                        this.targetWindow().postMessage( message, this.origin() );
    486                 },
    487 
    488                 bind: function( id, callback ) {
    489                         var topic = this.topics[ id ] || ( this.topics[ id ] = $.Callbacks() );
    490                         topic.add( callback );
    491                 },
    492 
    493                 unbind: function( id, callback ) {
    494                         if ( this.topics[ id ] )
    495                                 this.topics[ id ].remove( callback );
    496517                }
    497518        });
     519
     520        // Add the Events mixin to api.Messenger.
     521        $.extend( api.Messenger.prototype, api.Events );
    498522
    499523        /* =====================================================================
Note: See TracChangeset for help on using the changeset viewer.