]> source.dussan.org Git - jquery-ui.git/commitdiff
Widget: `instance()` should return `undefined` for empty sets
authorScott González <scott.gonzalez@gmail.com>
Thu, 4 Aug 2016 13:04:16 +0000 (09:04 -0400)
committerScott González <scott.gonzalez@gmail.com>
Thu, 4 Aug 2016 13:04:49 +0000 (09:04 -0400)
Fixes #15019

tests/unit/widget/core.js
ui/widget.js

index 00dcb16110ff06396b6504a7e60b6ffc264d1716..d6bb3efb813e9053bfe5138f666e1f7c2f456249 100644 (file)
@@ -786,7 +786,7 @@ QUnit.test( ".widget() - overriden", function( assert ) {
 } );
 
 QUnit.test( ".instance()", function( assert ) {
-       assert.expect( 2 );
+       assert.expect( 3 );
        var div;
 
        $.widget( "ui.testWidget", {
@@ -794,9 +794,11 @@ QUnit.test( ".instance()", function( assert ) {
        } );
 
        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 ) {
index c82b95326cb2b5cfb51ad05925288ec75e1fc4fc..06a3ce88e27225fe6072024f971c39a743474167 100644 (file)
@@ -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