Widget: instance() should return undefined for empty sets

Fixes #15019
This commit is contained in:
Scott González 2016-08-04 09:04:16 -04:00
parent 57537d09a4
commit 3dd8a09b44
2 changed files with 36 additions and 27 deletions

View File

@ -786,7 +786,7 @@ QUnit.test( ".widget() - overriden", function( assert ) {
} ); } );
QUnit.test( ".instance()", function( assert ) { QUnit.test( ".instance()", function( assert ) {
assert.expect( 2 ); assert.expect( 3 );
var div; var div;
$.widget( "ui.testWidget", { $.widget( "ui.testWidget", {
@ -794,9 +794,11 @@ QUnit.test( ".instance()", function( assert ) {
} ); } );
div = $( "<div>" ); div = $( "<div>" );
assert.equal( div.testWidget( "instance" ), undefined ); assert.equal( div.testWidget( "instance" ), undefined, "uninitialized" );
div.testWidget(); div.testWidget();
assert.equal( div.testWidget( "instance" ), div.testWidget( "instance" ) ); assert.equal( div.testWidget( "instance" ), div.testWidget( "instance" ), "initialized" );
assert.equal( $().testWidget( "instance" ), undefined, "empty set" );
} ); } );
QUnit.test( "._on() to element (default)", function( assert ) { QUnit.test( "._on() to element (default)", function( assert ) {

View File

@ -215,35 +215,42 @@ $.widget.bridge = function( name, object ) {
var returnValue = this; var returnValue = this;
if ( isMethodCall ) { if ( isMethodCall ) {
this.each( function() {
var methodValue;
var instance = $.data( this, fullName );
if ( options === "instance" ) { // If this is an empty collection, we need to have the instance method
returnValue = instance; // return undefined instead of the jQuery instance
return false; if ( !this.length && options === "instance" ) {
} returnValue = undefined;
} else {
this.each( function() {
var methodValue;
var instance = $.data( this, fullName );
if ( !instance ) { if ( options === "instance" ) {
return $.error( "cannot call methods on " + name + returnValue = instance;
" prior to initialization; " + return false;
"attempted to call method '" + options + "'" ); }
}
if ( !$.isFunction( instance[ options ] ) || options.charAt( 0 ) === "_" ) { if ( !instance ) {
return $.error( "no such method '" + options + "' for " + name + return $.error( "cannot call methods on " + name +
" widget instance" ); " prior to initialization; " +
} "attempted to call method '" + options + "'" );
}
methodValue = instance[ options ].apply( instance, args ); if ( !$.isFunction( instance[ options ] ) || options.charAt( 0 ) === "_" ) {
return $.error( "no such method '" + options + "' for " + name +
" widget instance" );
}
if ( methodValue !== instance && methodValue !== undefined ) { methodValue = instance[ options ].apply( instance, args );
returnValue = methodValue && methodValue.jquery ?
returnValue.pushStack( methodValue.get() ) : if ( methodValue !== instance && methodValue !== undefined ) {
methodValue; returnValue = methodValue && methodValue.jquery ?
return false; returnValue.pushStack( methodValue.get() ) :
} methodValue;
} ); return false;
}
} );
}
} else { } else {
// Allow multiple hashes to be passed on init // Allow multiple hashes to be passed on init