From: Scott González Date: Fri, 27 Aug 2010 18:48:17 +0000 (-0400) Subject: Widget: Throw errors when calling non-existent methods or methods on uninistantiated... X-Git-Tag: 1.8.5~46 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=1e28040cf358d0fe81ee83a2e35d7dbb65362afa;p=jquery-ui.git Widget: Throw errors when calling non-existent methods or methods on uninistantiated widgets. Fixes #5972 - Widget: Throw error for non-existent method calls. --- diff --git a/tests/unit/accordion/accordion_methods.js b/tests/unit/accordion/accordion_methods.js index 73faff27c..96688b59e 100644 --- a/tests/unit/accordion/accordion_methods.js +++ b/tests/unit/accordion/accordion_methods.js @@ -18,9 +18,6 @@ test("init", function() { $('
').appendTo('body').remove().accordion().remove(); ok(true, '.accordion() called on disconnected DOMElement - removed'); - $('
').accordion().accordion("foo").remove(); - ok(true, 'arbitrary method called after init'); - var el = $('
').accordion(); var foo = el.accordion("option", "foo"); el.remove(); diff --git a/tests/unit/dialog/dialog_core.js b/tests/unit/dialog/dialog_core.js index 4618417f0..aec3e22be 100644 --- a/tests/unit/dialog/dialog_core.js +++ b/tests/unit/dialog/dialog_core.js @@ -88,23 +88,6 @@ function margin(el, side) { module("dialog: core"); -test("element types", function() { - var typeNames = ('p,h1,h2,h3,h4,h5,h6,blockquote,ol,ul,dl,div,form' - + ',table,fieldset,address,ins,del,em,strong,q,cite,dfn,abbr' - + ',acronym,code,samp,kbd,var,img,object,hr' - + ',input,button,label,select,iframe').split(','); - - $.each(typeNames, function(i) { - var typeName = typeNames[i]; - el = $(document.createElement(typeName)).appendTo('body'); - (typeName == 'table' && el.append("content")); - el.dialog(); - ok(true, '$("<' + typeName + '/>").dialog()'); - el.dialog("destroy"); - el.remove(); - }); -}); - test("title id", function() { expect(3); diff --git a/tests/unit/dialog/dialog_methods.js b/tests/unit/dialog/dialog_methods.js index 93d1b25db..a961b4e03 100644 --- a/tests/unit/dialog/dialog_methods.js +++ b/tests/unit/dialog/dialog_methods.js @@ -10,7 +10,7 @@ module("dialog: methods", { }); test("init", function() { - expect(7); + expect(6); $("
").appendTo('body').dialog().remove(); ok(true, '.dialog() called on element'); @@ -24,9 +24,6 @@ test("init", function() { $('
').appendTo('body').remove().dialog().remove(); ok(true, '.dialog() called on disconnected DOMElement - removed'); - $('
').dialog().dialog("foo").remove(); - ok(true, 'arbitrary method called after init'); - el = $('
').dialog(); var foo = el.dialog("option", "foo"); el.remove(); @@ -46,9 +43,6 @@ test("destroy", function() { $('
').dialog().dialog("destroy").remove(); ok(true, '.dialog("destroy") called on disconnected DOMElement'); - $('
').dialog().dialog("destroy").dialog("foo").remove(); - ok(true, 'arbitrary method called after destroy'); - var expected = $('
').dialog(), actual = expected.dialog('destroy'); equals(actual, expected, 'destroy is chainable'); diff --git a/tests/unit/slider/slider.html b/tests/unit/slider/slider.html index 1ba5f76af..765340eb5 100644 --- a/tests/unit/slider/slider.html +++ b/tests/unit/slider/slider.html @@ -15,6 +15,7 @@ + diff --git a/tests/unit/slider/slider_methods.js b/tests/unit/slider/slider_methods.js index 567c5de2e..77ae65148 100644 --- a/tests/unit/slider/slider_methods.js +++ b/tests/unit/slider/slider_methods.js @@ -6,7 +6,7 @@ module("slider: methods"); test("init", function() { - expect(6); + expect(5); $("
").appendTo('body').slider().remove(); ok(true, '.slider() called on element'); @@ -17,9 +17,6 @@ test("init", function() { $('
').slider().remove(); ok(true, '.slider() called on disconnected DOMElement'); - $('
').slider().slider("foo").remove(); - ok(true, 'arbitrary method called after init'); - var el = $('
').slider(); var foo = el.slider("option", "foo"); el.remove(); @@ -39,9 +36,6 @@ test("destroy", function() { $('
').appendTo('body').remove().slider().slider("destroy").remove(); ok(true, '.slider("destroy") called on disconnected DOMElement'); - $('
').slider().slider("destroy").slider("foo").remove(); - ok(true, 'arbitrary method called after destroy'); - var expected = $('
').slider(), actual = expected.slider('destroy'); equals(actual, expected, 'destroy is chainable'); diff --git a/ui/jquery.ui.widget.js b/ui/jquery.ui.widget.js index ead4af12b..d08dbb81f 100644 --- a/ui/jquery.ui.widget.js +++ b/ui/jquery.ui.widget.js @@ -86,10 +86,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; + var instance = $.data( this, name ); + 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 ); if ( methodValue !== instance && methodValue !== undefined ) { returnValue = methodValue; return false;