Skip to content

Commit 9d88b56

Browse files
committed
Widget: Added _setOptions method for handling normalized options setting. Fixes #6114 - Widget: Add _setOptions() method.
1 parent 0b6710a commit 9d88b56

File tree

2 files changed

+33
-5
lines changed

2 files changed

+33
-5
lines changed

tests/unit/widget/widget_core.js

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,30 @@ test( ".option() - getter", function() {
209209
"modifying returned options hash does not modify plugin instance" );
210210
});
211211

212-
test( ".option() - setter", function() {
212+
test( ".option() - delegate to ._setOptions()", function() {
213+
var calls = [];
214+
$.widget( "ui.testWidget", {
215+
_create: function() {},
216+
_setOptions: function( options ) {
217+
calls.push( options );
218+
}
219+
});
220+
var div = $( "<div></div>" ).testWidget();
221+
222+
calls = [];
223+
div.testWidget( "option", "foo", "bar" );
224+
same( calls, [{ foo: "bar" }], "_setOptions called for single option" );
225+
226+
calls = [];
227+
div.testWidget( "option", {
228+
bar: "qux",
229+
quux: "quuux"
230+
});
231+
same( calls, [{ bar: "qux", quux: "quuux" }],
232+
"_setOptions called with multiple options" );
233+
});
234+
235+
test( ".option() - delegate to ._setOption()", function() {
213236
var calls = [];
214237
$.widget( "ui.testWidget", {
215238
_create: function() {},

ui/jquery.ui.widget.js

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -176,12 +176,11 @@ $.Widget.prototype = {
176176
},
177177

178178
option: function( key, value ) {
179-
var options = key,
180-
self = this;
179+
var options = key;
181180

182181
if ( arguments.length === 0 ) {
183182
// don't return a reference to the internal hash
184-
return $.extend( {}, self.options );
183+
return $.extend( {}, this.options );
185184
}
186185

187186
if (typeof key === "string" ) {
@@ -192,11 +191,17 @@ $.Widget.prototype = {
192191
options[ key ] = value;
193192
}
194193

194+
this._setOptions( options );
195+
196+
return this;
197+
},
198+
_setOptions: function( options ) {
199+
var self = this;
195200
$.each( options, function( key, value ) {
196201
self._setOption( key, value );
197202
});
198203

199-
return self;
204+
return this;
200205
},
201206
_setOption: function( key, value ) {
202207
this.options[ key ] = value;

0 commit comments

Comments
 (0)