]> source.dussan.org Git - jquery-ui.git/commitdiff
Widget: Allow setting individual properties of deep options. Fixes #7035 - Widget...
authorScott González <scott.gonzalez@gmail.com>
Thu, 24 Mar 2011 13:21:53 +0000 (09:21 -0400)
committerScott González <scott.gonzalez@gmail.com>
Thu, 24 Mar 2011 13:21:53 +0000 (09:21 -0400)
tests/unit/widget/widget_core.js
ui/jquery.ui.widget.js

index 300acd0d8bdb7f6878b81ec2cc37f3e8e294c85b..34f5ef67f4192826279e31cc9b5d90a61874e868 100644 (file)
@@ -441,6 +441,30 @@ test( ".option() - delegate to ._setOption()", function() {
        ], "_setOption called with multiple options" );
 });
 
+test( ".option() - deep option setter", function() {
+       $.widget( "ui.testWidget", {} );
+       var div = $( "<div>" ).testWidget();
+       function deepOption( from, to, msg ) {
+               div.data( "testWidget" ).options.foo = from;
+               $.ui.testWidget.prototype._setOption = function( key, value ) {
+                       same( key, "foo", msg + ": key" );
+                       same( value, to, msg + ": value" );
+               };
+       }
+
+       deepOption( { bar: "baz" }, { bar: "qux" }, "one deep" );
+       div.testWidget( "option", "foo.bar", "qux" );
+
+       deepOption( null, { bar: "baz" }, "null" );
+       div.testWidget( "option", "foo.bar", "baz" );
+
+       deepOption(
+               { bar: "baz", qux: { quux: "quuux" } },
+               { bar: "baz", qux: { quux: "quuux", newOpt: "newVal" } },
+               "add property" );
+       div.testWidget( "option", "foo.qux.newOpt", "newVal" );
+});
+
 test( ".enable()", function() {
        expect( 2 );
        $.widget( "ui.testWidget", {
index 09c680ec354d2d1feaee8d03adcf14bf1314578e..27d08ccd27f68f44e72bc5252be25ec7ea11d3f4 100644 (file)
@@ -208,19 +208,34 @@ $.Widget.prototype = {
        },
 
        option: function( key, value ) {
-               var options = key;
+               var options = key,
+                       parts,
+                       curOption,
+                       i;
 
                if ( arguments.length === 0 ) {
                        // don't return a reference to the internal hash
                        return $.extend( {}, this.options );
                }
 
-               if  (typeof key === "string" ) {
+               if typeof key === "string" ) {
                        if ( value === undefined ) {
                                return this.options[ key ];
                        }
+                       // handle nested keys, e.g., "foo.bar" => { foo: { bar: ___ } }
                        options = {};
-                       options[ key ] = value;
+                       parts = key.split( "." );
+                       key = parts.shift();
+                       if ( parts.length ) {
+                               curOption = options[ key ] = $.extend( true, {}, this.options[ key ] );
+                               for ( i = 0; i < parts.length - 1; i++ ) {
+                                       curOption[ parts[ i ] ] = curOption[ parts[ i ] ] || {};
+                                       curOption = curOption[ parts[ i ] ];
+                               }
+                               curOption[ parts.pop() ] = value;
+                       } else {
+                               options[ key ] = value;
+                       }
                }
 
                this._setOptions( options );