Skip to content

Commit

Permalink
Widget: Added ability to get deep options. Fixes #7459 - Widget: Exte…
Browse files Browse the repository at this point in the history
…nd .option() to get partial nested options.
  • Loading branch information
scottgonzalez committed Jun 9, 2011
1 parent 7cd3d0a commit 3dea8f1
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 5 deletions.
19 changes: 19 additions & 0 deletions tests/unit/widget/widget_core.js
Expand Up @@ -413,6 +413,7 @@ test( ".option() - getter", function() {
qux: [ "quux", "quuux" ]
});

same( div.testWidget( "option", "x" ), null, "non-existent option" );
same( div.testWidget( "option", "foo"), "bar", "single option - string" );
same( div.testWidget( "option", "baz"), 5, "single option - number" );
same( div.testWidget( "option", "qux"), [ "quux", "quuux" ],
Expand All @@ -431,6 +432,24 @@ test( ".option() - getter", function() {
"modifying returned options hash does not modify plugin instance" );
});

test( ".option() - deep option getter", function() {
$.widget( "ui.testWidget", {} );
var div = $( "<div>" ).testWidget({
foo: {
bar: "baz",
qux: {
quux: "xyzzy"
}
}
});
equal( div.testWidget( "option", "foo.bar" ), "baz", "one level deep - string" );
deepEqual( div.testWidget( "option", "foo.qux" ), { quux: "xyzzy" },
"one level deep - object" );
equal( div.testWidget( "option", "foo.qux.quux" ), "xyzzy", "two levels deep - string" );
equal( div.testWidget( "option", "x.y" ), null, "top level non-existent" );
equal( div.testWidget( "option", "foo.x.y" ), null, "one level deep - non-existent" );
});

test( ".option() - delegate to ._setOptions()", function() {
var calls = [];
$.widget( "ui.testWidget", {
Expand Down
14 changes: 9 additions & 5 deletions ui/jquery.ui.widget.js
Expand Up @@ -133,7 +133,7 @@ $.widget.bridge = function( name, object ) {
}
var methodValue = instance[ options ].apply( instance, args );
if ( methodValue !== instance && methodValue !== undefined ) {
returnValue = methodValue.jquery ?
returnValue = methodValue && methodValue.jquery ?
returnValue.pushStack( methodValue.get() ) :
methodValue;
return false;
Expand Down Expand Up @@ -239,9 +239,6 @@ $.Widget.prototype = {
}

if ( typeof key === "string" ) {
if ( value === undefined ) {
return this.options[ key ];
}
// handle nested keys, e.g., "foo.bar" => { foo: { bar: ___ } }
options = {};
parts = key.split( "." );
Expand All @@ -252,8 +249,15 @@ $.Widget.prototype = {
curOption[ parts[ i ] ] = curOption[ parts[ i ] ] || {};
curOption = curOption[ parts[ i ] ];
}
curOption[ parts.pop() ] = value;
key = parts.pop();
if ( value === undefined ) {
return curOption[ key ] === undefined ? null : curOption[ key ];
}
curOption[ key ] = value;
} else {
if ( value === undefined ) {
return this.options[ key ] === undefined ? null : this.options[ key ];
}
options[ key ] = value;
}
}
Expand Down

0 comments on commit 3dea8f1

Please sign in to comment.