Skip to content

Commit 3dea8f1

Browse files
committed
Widget: Added ability to get deep options. Fixes #7459 - Widget: Extend .option() to get partial nested options.
1 parent 7cd3d0a commit 3dea8f1

File tree

2 files changed

+28
-5
lines changed

2 files changed

+28
-5
lines changed

tests/unit/widget/widget_core.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -413,6 +413,7 @@ test( ".option() - getter", function() {
413413
qux: [ "quux", "quuux" ]
414414
});
415415

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

435+
test( ".option() - deep option getter", function() {
436+
$.widget( "ui.testWidget", {} );
437+
var div = $( "<div>" ).testWidget({
438+
foo: {
439+
bar: "baz",
440+
qux: {
441+
quux: "xyzzy"
442+
}
443+
}
444+
});
445+
equal( div.testWidget( "option", "foo.bar" ), "baz", "one level deep - string" );
446+
deepEqual( div.testWidget( "option", "foo.qux" ), { quux: "xyzzy" },
447+
"one level deep - object" );
448+
equal( div.testWidget( "option", "foo.qux.quux" ), "xyzzy", "two levels deep - string" );
449+
equal( div.testWidget( "option", "x.y" ), null, "top level non-existent" );
450+
equal( div.testWidget( "option", "foo.x.y" ), null, "one level deep - non-existent" );
451+
});
452+
434453
test( ".option() - delegate to ._setOptions()", function() {
435454
var calls = [];
436455
$.widget( "ui.testWidget", {

ui/jquery.ui.widget.js

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ $.widget.bridge = function( name, object ) {
133133
}
134134
var methodValue = instance[ options ].apply( instance, args );
135135
if ( methodValue !== instance && methodValue !== undefined ) {
136-
returnValue = methodValue.jquery ?
136+
returnValue = methodValue && methodValue.jquery ?
137137
returnValue.pushStack( methodValue.get() ) :
138138
methodValue;
139139
return false;
@@ -239,9 +239,6 @@ $.Widget.prototype = {
239239
}
240240

241241
if ( typeof key === "string" ) {
242-
if ( value === undefined ) {
243-
return this.options[ key ];
244-
}
245242
// handle nested keys, e.g., "foo.bar" => { foo: { bar: ___ } }
246243
options = {};
247244
parts = key.split( "." );
@@ -252,8 +249,15 @@ $.Widget.prototype = {
252249
curOption[ parts[ i ] ] = curOption[ parts[ i ] ] || {};
253250
curOption = curOption[ parts[ i ] ];
254251
}
255-
curOption[ parts.pop() ] = value;
252+
key = parts.pop();
253+
if ( value === undefined ) {
254+
return curOption[ key ] === undefined ? null : curOption[ key ];
255+
}
256+
curOption[ key ] = value;
256257
} else {
258+
if ( value === undefined ) {
259+
return this.options[ key ] === undefined ? null : this.options[ key ];
260+
}
257261
options[ key ] = value;
258262
}
259263
}

0 commit comments

Comments
 (0)