<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>[20344] trunk/wp-includes/js: Theme Customizer: Improve data binding in wp.customize.Value and wp.customize.Values.</title>
</head>
<body>

<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt;  }
#msg dl a { font-weight: bold}
#msg dl a:link    { color:#fc3; }
#msg dl a:active  { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { overflow: auto; background: #ffc; border: 1px #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg > ul, #logmsg > ol { margin-left: 0; margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff  {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<div id="msg">
<dl class="meta">
<dt>Revision</dt> <dd><a href="http://core.trac.wordpress.org/changeset/20344">20344</a></dd>
<dt>Author</dt> <dd>koopersmith</dd>
<dt>Date</dt> <dd>2012-04-03 22:04:40 +0000 (Tue, 03 Apr 2012)</dd>
</dl>

<h3>Log Message</h3>
<pre>Theme Customizer: Improve data binding in wp.customize.Value and wp.customize.Values. see <a href="http://core.trac.wordpress.org/ticket/19910">#19910</a>.

* Replace the convoluted wp.customize.Value.link method with a simple shortcut for direct binding.
* Add wp.customize.Value.sync for bidirectional linking.
* Add wp.customize.Value.setter for handling compound values (instead of using wp.customize.Value.link).</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#trunkwpincludesjscustomizebasedevjs">trunk/wp-includes/js/customize-base.dev.js</a></li>
<li><a href="#trunkwpincludesjscustomizecontrolsdevjs">trunk/wp-includes/js/customize-controls.dev.js</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunkwpincludesjscustomizebasedevjs"></a>
<div class="modfile"><h4>Modified: trunk/wp-includes/js/customize-base.dev.js (20343 => 20344)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/wp-includes/js/customize-base.dev.js        2012-04-02 22:58:19 UTC (rev 20343)
+++ trunk/wp-includes/js/customize-base.dev.js        2012-04-03 22:04:40 UTC (rev 20344)
</span><span class="lines">@@ -2,7 +2,7 @@
</span><span class="cx">         var wp = {};
</span><span class="cx"> 
</span><span class="cx"> (function( exports, $ ){
</span><del>-        var api, extend, ctor, inherits, ready,
</del><ins>+        var api, extend, ctor, inherits,
</ins><span class="cx">                 slice = Array.prototype.slice;
</span><span class="cx"> 
</span><span class="cx">         /* =====================================================================
</span><span class="lines">@@ -66,34 +66,7 @@
</span><span class="cx">                 return child;
</span><span class="cx">         };
</span><span class="cx"> 
</span><del>-        /* =====================================================================
-         * customize function.
-         * ===================================================================== */
-        ready = $.Callbacks( 'once memory' );
-
-        /*
-         * Sugar for main customize function. Supports several signatures.
-         *
-         * customize( callback, [context] );
-         *   Binds a callback to be fired when the customizer is ready.
-         *   - callback, function
-         *   - context, object
-         *
-         * customize( setting );
-         *   Fetches a setting object by ID.
-         *   - setting, string - The setting ID.
-         *
-         */
</del><span class="cx">         api = {};
</span><del>-        // api = function( callback, context ) {
-        //         if ( $.isFunction( callback ) ) {
-        //                 if ( context )
-        //                         callback = $.proxy( callback, context );
-        //                 ready.add( callback );
-        //
-        //                 return api;
-        //         }
-        // }
</del><span class="cx"> 
</span><span class="cx">         /* =====================================================================
</span><span class="cx">          * Base class.
</span><span class="lines">@@ -156,6 +129,8 @@
</span><span class="cx">                         this.callbacks = $.Callbacks();
</span><span class="cx"> 
</span><span class="cx">                         $.extend( this, options || {} );
</span><ins>+
+                        this.set = $.proxy( this.set, this );
</ins><span class="cx">                 },
</span><span class="cx"> 
</span><span class="cx">                 /*
</span><span class="lines">@@ -173,6 +148,7 @@
</span><span class="cx">                 set: function( to ) {
</span><span class="cx">                         var from = this._value;
</span><span class="cx"> 
</span><ins>+                        to = this._setter.apply( this, arguments );
</ins><span class="cx">                         to = this.validate( to );
</span><span class="cx"> 
</span><span class="cx">                         // Bail if the sanitized value is null or unchanged.
</span><span class="lines">@@ -186,6 +162,22 @@
</span><span class="cx">                         return this;
</span><span class="cx">                 },
</span><span class="cx"> 
</span><ins>+                _setter: function( to ) {
+                        return to;
+                },
+
+                setter: function( callback ) {
+                        this._setter = callback;
+                        this.set( this.get() );
+                        return this;
+                },
+
+                resetSetter: function() {
+                        this._setter = this.constructor.prototype._setter;
+                        this.set( this.get() );
+                        return this;
+                },
+
</ins><span class="cx">                 validate: function( value ) {
</span><span class="cx">                         return value;
</span><span class="cx">                 },
</span><span class="lines">@@ -200,183 +192,48 @@
</span><span class="cx">                         return this;
</span><span class="cx">                 },
</span><span class="cx"> 
</span><del>-                /*
-                 * Allows the creation of composite values.
-                 * Overrides the native link method (can be reverted with `unlink`).
-                 */
-                link: function() {
-                        var keys = slice.call( arguments ),
-                                callback = keys.pop(),
-                                self = this,
-                                set, key, active;
-
-                        if ( this.links )
-                                this.unlink();
-
-                        this.links = [];
-
-                        // Single argument means a direct binding.
-                        if ( ! keys.length ) {
-                                keys = [ callback ];
-                                callback = function( value, to ) {
-                                        return to;
-                                };
-                        }
-
-                        while ( key = keys.shift() ) {
-                                if ( this._parent &amp;&amp; $.type( key ) == 'string' )
-                                        this.links.push( this._parent[ key ] );
-                                else
-                                        this.links.push( key );
-                        }
-
-                        // Replace this.set with the assignment function.
-                        set = function() {
-                                var args, result;
-
-                                // If we call set from within the assignment function,
-                                // pass the arguments to the original set.
-                                if ( active )
-                                        return self.set.original.apply( self, arguments );
-
-                                active = true;
-
-                                args = self.links.concat( slice.call( arguments ) );
-                                result = callback.apply( self, args );
-
-                                active = false;
-
-                                if ( typeof result !== 'undefined' )
-                                        self.set.original.call( self, result );
-                        };
-
-                        set.original = this.set;
-                        this.set = set;
-
-                        // Bind the new function to the master values.
-                        $.each( this.links, function( key, value ) {
-                                value.bind( self.set );
</del><ins>+                link: function() { // values*
+                        var set = this.set;
+                        $.each( arguments, function() {
+                                this.bind( set );
</ins><span class="cx">                         });
</span><del>-
-                        this.set( this.get() );
-
</del><span class="cx">                         return this;
</span><span class="cx">                 },
</span><span class="cx"> 
</span><del>-                unlink: function() {
</del><ins>+                unlink: function() { // values*
</ins><span class="cx">                         var set = this.set;
</span><del>-
-                        $.each( this.links, function( key, value ) {
-                                value.unbind( set );
</del><ins>+                        $.each( arguments, function() {
+                                this.unbind( set );
</ins><span class="cx">                         });
</span><del>-
-                        delete this.links;
-                        this.set = this.set.original;
</del><span class="cx">                         return this;
</span><del>-                }
-        });
-
-        api.ensure = function( element ) {
-                return typeof element == 'string' ? $( element ) : element;
-        };
-
-        api.Element = api.Value.extend({
-                initialize: function( element, options ) {
-                        var self = this,
-                                synchronizer = api.Element.synchronizer.html,
-                                type, update, refresh;
-
-                        this.element = api.ensure( element );
-                        this.events = '';
-
-                        if ( this.element.is('input, select, textarea') ) {
-                                this.events += 'change';
-                                synchronizer = api.Element.synchronizer.val;
-
-                                if ( this.element.is('input') ) {
-                                        type = this.element.prop('type');
-                                        if ( api.Element.synchronizer[ type ] )
-                                                synchronizer = api.Element.synchronizer[ type ];
-                                        if ( 'text' === type || 'password' === type )
-                                                this.events += ' keyup';
-                                }
-                        }
-
-                        api.Value.prototype.initialize.call( this, null, $.extend( options || {}, synchronizer ) );
-                        this._value = this.get();
-
-                        update  = this.update;
-                        refresh = this.refresh;
-
-                        this.update = function( to ) {
-                                if ( to !== refresh.call( self ) )
-                                        update.apply( this, arguments );
-                        };
-                        this.refresh = function() {
-                                self.set( refresh.call( self ) );
-                        };
-
-                        this.bind( this.update );
-                        this.element.bind( this.events, this.refresh );
</del><span class="cx">                 },
</span><span class="cx"> 
</span><del>-                find: function( selector ) {
-                        return $( selector, this.element );
</del><ins>+                sync: function() { // values*
+                        var that = this;
+                        $.each( arguments, function() {
+                                that.link( this );
+                                this.link( that );
+                        });
+                        return this;
</ins><span class="cx">                 },
</span><span class="cx"> 
</span><del>-                refresh: function() {},
-                update: function() {}
</del><ins>+                unsync: function() { // values*
+                        var that = this;
+                        $.each( arguments, function() {
+                                that.unlink( this );
+                                this.unlink( that );
+                        });
+                        return this;
+                }
</ins><span class="cx">         });
</span><span class="cx"> 
</span><del>-        api.Element.synchronizer = {};
-
-        $.each( [ 'html', 'val' ], function( i, method ) {
-                api.Element.synchronizer[ method ] = {
-                        update: function( to ) {
-                                this.element[ method ]( to );
-                        },
-                        refresh: function() {
-                                return this.element[ method ]();
-                        }
-                };
-        });
-
-        api.Element.synchronizer.checkbox = {
-                update: function( to ) {
-                        this.element.prop( 'checked', to );
-                },
-                refresh: function() {
-                        return this.element.prop( 'checked' );
-                }
-        };
-
-        api.Element.synchronizer.radio = {
-                update: function( to ) {
-                        this.element.filter( function() {
-                                return this.value === to;
-                        }).prop( 'checked', true );
-                },
-                refresh: function() {
-                        return this.element.filter( ':checked' ).val();
-                }
-        };
-
-        api.ValueFactory = function( constructor ) {
-                constructor = constructor || api.Value;
-
-                return function( key ) {
-                        var args = slice.call( arguments, 1 );
-                        this[ key ] = new constructor( api.Class.applicator, args );
-                        this[ key ]._parent = this;
-                        return this[ key ];
-                };
-        };
-
-        api.Values = api.Value.extend({
</del><ins>+        api.Values = api.Class.extend({
</ins><span class="cx">                 defaultConstructor: api.Value,
</span><span class="cx"> 
</span><span class="cx">                 initialize: function( options ) {
</span><del>-                        api.Value.prototype.initialize.call( this, {}, options || {} );
</del><ins>+                        $.extend( this, options || {} );
+
+                        this._value = {};
</ins><span class="cx">                         this._deferreds = {};
</span><span class="cx">                 },
</span><span class="cx"> 
</span><span class="lines">@@ -400,7 +257,7 @@
</span><span class="cx">                                 return this.value( id );
</span><span class="cx"> 
</span><span class="cx">                         this._value[ id ] = value;
</span><del>-                        this._value[ id ]._parent = this._value;
</del><ins>+                        this._value[ id ].parent = this;
</ins><span class="cx"> 
</span><span class="cx">                         if ( this._deferreds[ id ] )
</span><span class="cx">                                 this._deferreds[ id ].resolve();
</span><span class="lines">@@ -469,32 +326,121 @@
</span><span class="cx">                 }
</span><span class="cx">         });
</span><span class="cx"> 
</span><del>-        $.each( [ 'get', 'bind', 'unbind', 'link', 'unlink' ], function( i, method ) {
</del><ins>+        $.each( [ 'get', 'bind', 'unbind', 'link', 'unlink', 'sync', 'unsync', 'setter', 'resetSetter' ], function( i, method ) {
</ins><span class="cx">                 api.Values.prototype[ method ] = function() {
</span><span class="cx">                         return this.pass( method, arguments );
</span><span class="cx">                 };
</span><span class="cx">         });
</span><span class="cx"> 
</span><ins>+        api.ensure = function( element ) {
+                return typeof element == 'string' ? $( element ) : element;
+        };
+
+        api.Element = api.Value.extend({
+                initialize: function( element, options ) {
+                        var self = this,
+                                synchronizer = api.Element.synchronizer.html,
+                                type, update, refresh;
+
+                        this.element = api.ensure( element );
+                        this.events = '';
+
+                        if ( this.element.is('input, select, textarea') ) {
+                                this.events += 'change';
+                                synchronizer = api.Element.synchronizer.val;
+
+                                if ( this.element.is('input') ) {
+                                        type = this.element.prop('type');
+                                        if ( api.Element.synchronizer[ type ] )
+                                                synchronizer = api.Element.synchronizer[ type ];
+                                        if ( 'text' === type || 'password' === type )
+                                                this.events += ' keyup';
+                                }
+                        }
+
+                        api.Value.prototype.initialize.call( this, null, $.extend( options || {}, synchronizer ) );
+                        this._value = this.get();
+
+                        update  = this.update;
+                        refresh = this.refresh;
+
+                        this.update = function( to ) {
+                                if ( to !== refresh.call( self ) )
+                                        update.apply( this, arguments );
+                        };
+                        this.refresh = function() {
+                                self.set( refresh.call( self ) );
+                        };
+
+                        this.bind( this.update );
+                        this.element.bind( this.events, this.refresh );
+                },
+
+                find: function( selector ) {
+                        return $( selector, this.element );
+                },
+
+                refresh: function() {},
+
+                update: function() {}
+        });
+
+        api.Element.synchronizer = {};
+
+        $.each( [ 'html', 'val' ], function( i, method ) {
+                api.Element.synchronizer[ method ] = {
+                        update: function( to ) {
+                                this.element[ method ]( to );
+                        },
+                        refresh: function() {
+                                return this.element[ method ]();
+                        }
+                };
+        });
+
+        api.Element.synchronizer.checkbox = {
+                update: function( to ) {
+                        this.element.prop( 'checked', to );
+                },
+                refresh: function() {
+                        return this.element.prop( 'checked' );
+                }
+        };
+
+        api.Element.synchronizer.radio = {
+                update: function( to ) {
+                        this.element.filter( function() {
+                                return this.value === to;
+                        }).prop( 'checked', true );
+                },
+                refresh: function() {
+                        return this.element.filter( ':checked' ).val();
+                }
+        };
+
</ins><span class="cx">         /* =====================================================================
</span><span class="cx">          * Messenger for postMessage.
</span><span class="cx">          * ===================================================================== */
</span><span class="cx"> 
</span><span class="cx">         api.Messenger = api.Class.extend({
</span><del>-                add: api.ValueFactory(),
</del><ins>+                add: function( key, initial, options ) {
+                        return this[ key ] = new api.Value( initial, options );
+                },
</ins><span class="cx"> 
</span><span class="cx">                 initialize: function( url, targetWindow, options ) {
</span><span class="cx">                         $.extend( this, options || {} );
</span><span class="cx"> 
</span><del>-                        this.add( 'url', url );
</del><ins>+                        url = this.add( 'url', url );
</ins><span class="cx">                         this.add( 'targetWindow', targetWindow || null );
</span><del>-                        this.add( 'origin' ).link( 'url', function( url ) {
-                                return url().replace( /([^:]+:\/\/[^\/]+).*/, '$1' );
</del><ins>+                        this.add( 'origin', url() ).link( url ).setter( function( to ) {
+                                return to.replace( /([^:]+:\/\/[^\/]+).*/, '$1' );
</ins><span class="cx">                         });
</span><span class="cx"> 
</span><span class="cx">                         this.topics = {};
</span><span class="cx"> 
</span><span class="cx">                         $.receiveMessage( $.proxy( this.receive, this ), this.origin() || null );
</span><span class="cx">                 },
</span><ins>+
</ins><span class="cx">                 receive: function( event ) {
</span><span class="cx">                         var message;
</span><span class="cx"> 
</span><span class="lines">@@ -507,6 +453,7 @@
</span><span class="cx">                         if ( message &amp;&amp; message.id &amp;&amp; message.data &amp;&amp; this.topics[ message.id ] )
</span><span class="cx">                                 this.topics[ message.id ].fireWith( this, [ message.data ]);
</span><span class="cx">                 },
</span><ins>+
</ins><span class="cx">                 send: function( id, data ) {
</span><span class="cx">                         var message;
</span><span class="cx"> 
</span><span class="lines">@@ -516,10 +463,12 @@
</span><span class="cx">                         message = JSON.stringify({ id: id, data: data });
</span><span class="cx">                         $.postMessage( message, this.url(), this.targetWindow() );
</span><span class="cx">                 },
</span><ins>+
</ins><span class="cx">                 bind: function( id, callback ) {
</span><span class="cx">                         var topic = this.topics[ id ] || ( this.topics[ id ] = $.Callbacks() );
</span><span class="cx">                         topic.add( callback );
</span><span class="cx">                 },
</span><ins>+
</ins><span class="cx">                 unbind: function( id, callback ) {
</span><span class="cx">                         if ( this.topics[ id ] )
</span><span class="cx">                                 this.topics[ id ].remove( callback );
</span></span></pre></div>
<a id="trunkwpincludesjscustomizecontrolsdevjs"></a>
<div class="modfile"><h4>Modified: trunk/wp-includes/js/customize-controls.dev.js (20343 => 20344)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/wp-includes/js/customize-controls.dev.js        2012-04-02 22:58:19 UTC (rev 20343)
+++ trunk/wp-includes/js/customize-controls.dev.js        2012-04-03 22:04:40 UTC (rev 20344)
</span><span class="lines">@@ -4,7 +4,7 @@
</span><span class="cx">         /*
</span><span class="cx">          * @param options
</span><span class="cx">          * - previewer - The Previewer instance to sync with.
</span><del>-         * - method    - The method to use for syncing. Supports 'refresh' and 'postMessage'.
</del><ins>+         * - method    - The method to use for previewing. Supports 'refresh' and 'postMessage'.
</ins><span class="cx">          */
</span><span class="cx">         api.Setting = api.Value.extend({
</span><span class="cx">                 initialize: function( id, value, options ) {
</span><span class="lines">@@ -24,12 +24,10 @@
</span><span class="cx">                         element.appendTo( this.previewer.form );
</span><span class="cx">                         this.element = new api.Element( element );
</span><span class="cx"> 
</span><del>-                        this.element.link( this );
-                        this.link( this.element );
-
-                        this.bind( this.sync );
</del><ins>+                        this.sync( this.element );
+                        this.bind( this.preview );
</ins><span class="cx">                 },
</span><del>-                sync: function() {
</del><ins>+                preview: function() {
</ins><span class="cx">                         switch ( this.method ) {
</span><span class="cx">                                 case 'refresh':
</span><span class="cx">                                         return this.previewer.refresh();
</span><span class="lines">@@ -88,9 +86,8 @@
</span><span class="cx">                                 api( node.data('customizeSettingLink'), function( setting ) {
</span><span class="cx">                                         var element = new api.Element( node );
</span><span class="cx">                                         control.elements.push( element );
</span><del>-                                        element.link( setting ).bind( function( to ) {
-                                                setting( to );
-                                        });
</del><ins>+                                        element.sync( setting );
+                                        element.set( setting() );
</ins><span class="cx">                                 });
</span><span class="cx">                         });
</span><span class="cx">                 },
</span><span class="lines">@@ -122,10 +119,6 @@
</span><span class="cx">                         this.setting.bind( update );
</span><span class="cx">                         update( this.setting() );
</span><span class="cx">                 }
</span><del>-                // ,
-                //                 validate: function( to ) {
-                //                         return /^[a-fA-F0-9]{3}([a-fA-F0-9]{3})?$/.test( to ) ? to : null;
-                //                 }
</del><span class="cx">         });
</span><span class="cx"> 
</span><span class="cx">         api.UploadControl = api.Control.extend({
</span><span class="lines">@@ -400,7 +393,7 @@
</span><span class="cx">                 api.control( 'display_header_text', function( control ) {
</span><span class="cx">                         var last = '';
</span><span class="cx"> 
</span><del>-                        control.elements[0].unlink();
</del><ins>+                        control.elements[0].unsync( api( 'header_textcolor' ) );
</ins><span class="cx"> 
</span><span class="cx">                         control.element = new api.Element( control.container.find('input') );
</span><span class="cx">                         control.element.set( 'blank' !== control.setting() );
</span></span></pre>
</div>
</div>

</body>
</html>