Skip to content

Commit 3dd8a09

Browse files
committed
Widget: instance() should return undefined for empty sets
Fixes #15019
1 parent 57537d0 commit 3dd8a09

File tree

2 files changed

+36
-27
lines changed

2 files changed

+36
-27
lines changed

tests/unit/widget/core.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -786,17 +786,19 @@ QUnit.test( ".widget() - overriden", function( assert ) {
786786
} );
787787

788788
QUnit.test( ".instance()", function( assert ) {
789-
assert.expect( 2 );
789+
assert.expect( 3 );
790790
var div;
791791

792792
$.widget( "ui.testWidget", {
793793
_create: function() {}
794794
} );
795795

796796
div = $( "<div>" );
797-
assert.equal( div.testWidget( "instance" ), undefined );
797+
assert.equal( div.testWidget( "instance" ), undefined, "uninitialized" );
798798
div.testWidget();
799-
assert.equal( div.testWidget( "instance" ), div.testWidget( "instance" ) );
799+
assert.equal( div.testWidget( "instance" ), div.testWidget( "instance" ), "initialized" );
800+
801+
assert.equal( $().testWidget( "instance" ), undefined, "empty set" );
800802
} );
801803

802804
QUnit.test( "._on() to element (default)", function( assert ) {

ui/widget.js

Lines changed: 31 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -215,35 +215,42 @@ $.widget.bridge = function( name, object ) {
215215
var returnValue = this;
216216

217217
if ( isMethodCall ) {
218-
this.each( function() {
219-
var methodValue;
220-
var instance = $.data( this, fullName );
221218

222-
if ( options === "instance" ) {
223-
returnValue = instance;
224-
return false;
225-
}
219+
// If this is an empty collection, we need to have the instance method
220+
// return undefined instead of the jQuery instance
221+
if ( !this.length && options === "instance" ) {
222+
returnValue = undefined;
223+
} else {
224+
this.each( function() {
225+
var methodValue;
226+
var instance = $.data( this, fullName );
226227

227-
if ( !instance ) {
228-
return $.error( "cannot call methods on " + name +
229-
" prior to initialization; " +
230-
"attempted to call method '" + options + "'" );
231-
}
228+
if ( options === "instance" ) {
229+
returnValue = instance;
230+
return false;
231+
}
232232

233-
if ( !$.isFunction( instance[ options ] ) || options.charAt( 0 ) === "_" ) {
234-
return $.error( "no such method '" + options + "' for " + name +
235-
" widget instance" );
236-
}
233+
if ( !instance ) {
234+
return $.error( "cannot call methods on " + name +
235+
" prior to initialization; " +
236+
"attempted to call method '" + options + "'" );
237+
}
237238

238-
methodValue = instance[ options ].apply( instance, args );
239+
if ( !$.isFunction( instance[ options ] ) || options.charAt( 0 ) === "_" ) {
240+
return $.error( "no such method '" + options + "' for " + name +
241+
" widget instance" );
242+
}
239243

240-
if ( methodValue !== instance && methodValue !== undefined ) {
241-
returnValue = methodValue && methodValue.jquery ?
242-
returnValue.pushStack( methodValue.get() ) :
243-
methodValue;
244-
return false;
245-
}
246-
} );
244+
methodValue = instance[ options ].apply( instance, args );
245+
246+
if ( methodValue !== instance && methodValue !== undefined ) {
247+
returnValue = methodValue && methodValue.jquery ?
248+
returnValue.pushStack( methodValue.get() ) :
249+
methodValue;
250+
return false;
251+
}
252+
} );
253+
}
247254
} else {
248255

249256
// Allow multiple hashes to be passed on init

0 commit comments

Comments
 (0)