]> source.dussan.org Git - jquery-ui.git/commitdiff
Widget: Throw errors for invalid method calls. Fixes #5972 - Widget: Throw error...
authorScott González <scott.gonzalez@gmail.com>
Fri, 10 Dec 2010 19:11:20 +0000 (14:11 -0500)
committerScott González <scott.gonzalez@gmail.com>
Fri, 10 Dec 2010 19:11:20 +0000 (14:11 -0500)
tests/unit/widget/widget_core.js
ui/jquery.ui.widget.js

index 2673350f3c8bf17fa7bcc312eff3e2dee6dd6fcd..c01cd65428e00b2d60820d09bd1a51ed37428905 100644 (file)
@@ -120,6 +120,23 @@ test( "direct usage", function() {
        equals( instance.getterSetterVal, 30, "getter/setter can act as setter" );
 });
 
+test( "error handling", function() {
+       expect( 2 );
+       var error = $.error;
+       $.widget( "ui.testWidget", {} );
+       $.error = function( msg ) {
+               equal( msg, "cannot call methods on testWidget prior to initialization; " +
+                       "attempted to call method 'missing'", "method call before init" );
+       };
+       $( "<div>" ).testWidget( "missing" );
+       $.error = function( msg ) {
+               equal( msg, "no such method 'missing' for testWidget widget instance",
+                       "invalid method call on widget instance" );
+       };
+       $( "<div>" ).testWidget().testWidget( "missing" );
+       $.error = error;
+});
+
 test("merge multiple option arguments", function() {
        expect( 1 );
        $.widget( "ui.testWidget", {
index fadf81f2be427da7e9615cd8bc3326a22d6181e6..0420bc31ae920360dab8afd3bae09562cc669c66 100644 (file)
@@ -96,19 +96,15 @@ $.widget.bridge = function( name, object ) {
 
                if ( isMethodCall ) {
                        this.each(function() {
-                               var instance = $.data( this, name ),
-                                       methodValue = instance && $.isFunction( instance[options] ) ?
-                                               instance[ options ].apply( instance, args ) :
-                                               instance;
-                               // TODO: add this back in 1.9 and use $.error() (see #5972)
-//                             if ( !instance ) {
-//                                     throw "cannot call methods on " + name + " prior to initialization; " +
-//                                             "attempted to call method '" + options + "'";
-//                             }
-//                             if ( !$.isFunction( instance[options] ) ) {
-//                                     throw "no such method '" + options + "' for " + name + " widget instance";
-//                             }
-//                             var methodValue = instance[ options ].apply( instance, args );
+                               var instance = $.data( this, name );
+                               if ( !instance ) {
+                                       return $.error( "cannot call methods on " + name + " prior to initialization; " +
+                                               "attempted to call method '" + options + "'" );
+                               }
+                               if ( !$.isFunction( instance[options] ) ) {
+                                       return $.error( "no such method '" + options + "' for " + name + " widget instance" );
+                               }
+                               var methodValue = instance[ options ].apply( instance, args );
                                if ( methodValue !== instance && methodValue !== undefined ) {
                                        returnValue = methodValue;
                                        return false;