Permalink
Browse files

Widget: `instance()` should return `undefined` for empty sets

Fixes #15019
  • Loading branch information...
scottgonzalez committed Aug 4, 2016
1 parent 57537d0 commit 3dd8a09b441d65445f2b6a7c73e72af65445d5da
Showing with 36 additions and 27 deletions.
  1. +5 −3 tests/unit/widget/core.js
  2. +31 −24 ui/widget.js
@@ -786,17 +786,19 @@ QUnit.test( ".widget() - overriden", function( assert ) {
} );
QUnit.test( ".instance()", function( assert ) {
assert.expect( 2 );
assert.expect( 3 );
var div;
$.widget( "ui.testWidget", {
_create: function() {}
} );
div = $( "<div>" );
assert.equal( div.testWidget( "instance" ), undefined );
assert.equal( div.testWidget( "instance" ), undefined, "uninitialized" );
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 ) {
View
@@ -215,35 +215,42 @@ $.widget.bridge = function( name, object ) {
var returnValue = this;
if ( isMethodCall ) {
this.each( function() {
var methodValue;
var instance = $.data( this, fullName );
if ( options === "instance" ) {
returnValue = instance;
return false;
}
// If this is an empty collection, we need to have the instance method
// return undefined instead of the jQuery instance
if ( !this.length && options === "instance" ) {
returnValue = undefined;
} else {
this.each( function() {
var methodValue;
var instance = $.data( this, fullName );
if ( !instance ) {
return $.error( "cannot call methods on " + name +
" prior to initialization; " +
"attempted to call method '" + options + "'" );
}
if ( options === "instance" ) {
returnValue = instance;
return false;
}
if ( !$.isFunction( instance[ options ] ) || options.charAt( 0 ) === "_" ) {
return $.error( "no such method '" + options + "' for " + name +
" widget instance" );
}
if ( !instance ) {
return $.error( "cannot call methods on " + name +
" 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 ) {
returnValue = methodValue && methodValue.jquery ?
returnValue.pushStack( methodValue.get() ) :
methodValue;
return false;
}
} );
methodValue = instance[ options ].apply( instance, args );
if ( methodValue !== instance && methodValue !== undefined ) {
returnValue = methodValue && methodValue.jquery ?
returnValue.pushStack( methodValue.get() ) :
methodValue;
return false;
}
} );
}
} else {
// Allow multiple hashes to be passed on init

0 comments on commit 3dd8a09

Please sign in to comment.