Skip to content

Commit

Permalink
Widget: option-method should work as getter only when argument length…
Browse files Browse the repository at this point in the history
… is 1. Fixes #9601 - Widget: calling _setOption with undefined as 3rd argument makes it a getter

(cherry picked from commit ecde7cd)
  • Loading branch information
dekajp authored and scottgonzalez committed Nov 26, 2013
1 parent 8eefd20 commit 6cdebe8
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 5 deletions.
17 changes: 14 additions & 3 deletions tests/unit/widget/widget_core.js
Expand Up @@ -535,7 +535,7 @@ test( ".option() - delegate to ._setOptions()", function() {
});

test( ".option() - delegate to ._setOption()", function() {
expect( 2 );
expect( 3 );
var div,
calls = [];
$.widget( "ui.testWidget", {
Expand All @@ -554,6 +554,11 @@ test( ".option() - delegate to ._setOption()", function() {
deepEqual( calls, [{ key: "foo", val: "bar" }],
"_setOption called for single option" );

calls = [];
div.testWidget( "option", "foo", undefined );
deepEqual( calls, [{ key: "foo", val: undefined }],
"_setOption called for single option where value is undefined" );

calls = [];
div.testWidget( "option", {
bar: "qux",
Expand All @@ -566,9 +571,9 @@ test( ".option() - delegate to ._setOption()", function() {
});

test( ".option() - deep option setter", function() {
expect( 6 );
expect( 9 );
$.widget( "ui.testWidget", {} );
var div = $( "<div>" ).testWidget();
var result, div = $( "<div>" ).testWidget();
function deepOption( from, to, msg ) {
div.data( "ui-testWidget" ).options.foo = from;
$.ui.testWidget.prototype._setOption = function( key, value ) {
Expand All @@ -580,6 +585,12 @@ test( ".option() - deep option setter", function() {
deepOption( { bar: "baz" }, { bar: "qux" }, "one deep" );
div.testWidget( "option", "foo.bar", "qux" );

deepOption( { bar: "baz" }, { bar: undefined }, "one deep - value = undefined" );

result = div.testWidget( "option", "foo.bar", undefined );

deepEqual ( result, div, "option should return widget on successful set operation" );

deepOption( null, { bar: "baz" }, "null" );
div.testWidget( "option", "foo.bar", "baz" );

Expand Down
4 changes: 2 additions & 2 deletions ui/jquery.ui.widget.js
Expand Up @@ -315,12 +315,12 @@ $.Widget.prototype = {
curOption = curOption[ parts[ i ] ];
}
key = parts.pop();
if ( value === undefined ) {
if ( arguments.length === 1 ) {
return curOption[ key ] === undefined ? null : curOption[ key ];
}
curOption[ key ] = value;
} else {
if ( value === undefined ) {
if ( arguments.length === 1 ) {
return this.options[ key ] === undefined ? null : this.options[ key ];
}
options[ key ] = value;
Expand Down

0 comments on commit 6cdebe8

Please sign in to comment.