Skip to content

Commit 9ad2a4b

Browse files
committed
Widget: Throw errors for invalid method calls. Fixes #5972 - Widget: Throw error for non-existent method calls.
1 parent 0e15f57 commit 9ad2a4b

File tree

2 files changed

+26
-13
lines changed

2 files changed

+26
-13
lines changed

tests/unit/widget/widget_core.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,23 @@ test( "direct usage", function() {
120120
equals( instance.getterSetterVal, 30, "getter/setter can act as setter" );
121121
});
122122

123+
test( "error handling", function() {
124+
expect( 2 );
125+
var error = $.error;
126+
$.widget( "ui.testWidget", {} );
127+
$.error = function( msg ) {
128+
equal( msg, "cannot call methods on testWidget prior to initialization; " +
129+
"attempted to call method 'missing'", "method call before init" );
130+
};
131+
$( "<div>" ).testWidget( "missing" );
132+
$.error = function( msg ) {
133+
equal( msg, "no such method 'missing' for testWidget widget instance",
134+
"invalid method call on widget instance" );
135+
};
136+
$( "<div>" ).testWidget().testWidget( "missing" );
137+
$.error = error;
138+
});
139+
123140
test("merge multiple option arguments", function() {
124141
expect( 1 );
125142
$.widget( "ui.testWidget", {

ui/jquery.ui.widget.js

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -96,19 +96,15 @@ $.widget.bridge = function( name, object ) {
9696

9797
if ( isMethodCall ) {
9898
this.each(function() {
99-
var instance = $.data( this, name ),
100-
methodValue = instance && $.isFunction( instance[options] ) ?
101-
instance[ options ].apply( instance, args ) :
102-
instance;
103-
// TODO: add this back in 1.9 and use $.error() (see #5972)
104-
// if ( !instance ) {
105-
// throw "cannot call methods on " + name + " prior to initialization; " +
106-
// "attempted to call method '" + options + "'";
107-
// }
108-
// if ( !$.isFunction( instance[options] ) ) {
109-
// throw "no such method '" + options + "' for " + name + " widget instance";
110-
// }
111-
// var methodValue = instance[ options ].apply( instance, args );
99+
var instance = $.data( this, name );
100+
if ( !instance ) {
101+
return $.error( "cannot call methods on " + name + " prior to initialization; " +
102+
"attempted to call method '" + options + "'" );
103+
}
104+
if ( !$.isFunction( instance[options] ) ) {
105+
return $.error( "no such method '" + options + "' for " + name + " widget instance" );
106+
}
107+
var methodValue = instance[ options ].apply( instance, args );
112108
if ( methodValue !== instance && methodValue !== undefined ) {
113109
returnValue = methodValue;
114110
return false;

0 commit comments

Comments
 (0)