Opened 13 years ago

Closed 13 years ago

#5972 closed enhancement (fixed)

Widget: Throw error for non-existent method calls

Reported by: keturn Owned by:
Priority: minor Milestone: 1.9.0
Component: ui.widget Version: 1.8.4
Keywords: Cc:
Blocked by: Blocking:

Description (last modified by Scott González)

$( el ).plugin( invalidMethod ) should throw an exception. Calling a method before initialization should throw an exception as well.

Change History (14)

comment:1 Changed 13 years ago by Scott González

Component: ui.autocompleteui.widget
Description: modified (diff)
Summary: should issue warning or error when invoking the wrong methodWidget: Throw error for non-existant method calls

comment:2 Changed 13 years ago by rdworth

Summary: Widget: Throw error for non-existant method callsWidget: Throw error for non-existent method calls

comment:3 Changed 13 years ago by rdworth

I'm as hesitant to implement this as I would be to throw an exception

  • if a method receives an argument of a different type than is expected
  • if a method receives a different number of arguments than expected
  • if an option is set/get that the original plugin author is not aware of

These provides no benefit at run-time, only at development/debug time and all have serious potential to limit extensibility.

We can draw a parallel (even if stretched) to the oft-requested "feature" that jQuery Core throw an exception if any jQuery method is called against an empty set. That is surely not an exception, but in certain circumstances people feel like they would have had time saved if they were made aware of its occurrence. And while I recognize such a direct comparison is a stretch (closer would be calling an undefined method in JavaScript, which is an exception), I'm careful to preserve what we may have currently as powerful rather than dangerous.

Just as an example, declaring it an exception to call a non-existent widget method would preempt someone from creating a noSuchMethod implementation for jQuery UI widgets, something that is currently a real possibility, and perhaps even interesting.

If this is so sorely needed at development/debug time, couldn't it simply be opted in via a debug plugin? Such a debug plugin would be very easy to write if we were to build in a noSuchMethod hook but difficult (if even possible) to write if we declared such a function call an exception.

comment:4 Changed 13 years ago by Scott González

I'm also hesitant to add any throwing at such a high level. When I saw the ticket, my first thought was to actually create a noSuchMethod method and allow that to handle it however you want. The default implementation could just be an empty function. As for calling methods on uninstantiated plugins, we may decide to auto-instantiate with default options in the future.

comment:5 Changed 13 years ago by keturn

Well, there's clearly some background information I'm missing, because I don't understand why plugin methods go like $(selector).widget('method') instead of $(selector).widget.method() like the rest of jQuery and JavaScript, but, as for this:

Just as an example, declaring it an exception to call a non-existent widget method would preempt someone from creating a noSuchMethod implementation for jQuery UI widgets, something that is currently a real possibility, and perhaps even interesting.

This doesn't seem to be a problem for Python or Ruby. They have no problems with giving me an error when I try to invoke a method that doesn't exist, but offer __getattr__ and method_missing if I want another behavior.

My expectation as a developer is that a method is going to behave like a JavaScript method, and the JS engines I use will give me an error if I try to call something that's undefined. Don't break that expectation for a "someday we might" feature. And we don't need *more* ways to shoot ourselves in our foot.

comment:6 Changed 13 years ago by Scott González

Milestone: TBD1.9
Resolution: fixed
Status: newclosed

Fixed in 1e28040.

comment:7 Changed 13 years ago by Scott González

Milestone: 1.91.8.5

comment:8 Changed 13 years ago by be.davestein

Resolution: fixed
Status: closedreopened

Hey Scott -

I see you commented 2 months ago that you did not want to throw an exception so high up, which I agree with. Then it had a milestone for 1.9, the next major release, which is debatable in itself. Then it somehow got into the minor release of 1.8.5 therefore being the first UI minor release that has broken my application. For example, the upgrade guide only says 1.7 to 1.8. There is no warning of a harmless return false changing to throwing an exception in ui.widget.

I'm definitely on board with an exception for a non-existent method because that is standard - you can't call what doesn't exist at all ever. Throwing an exception before something is initialized seems to me like making $('#notonpageyet').height() throw an exception.

What you guys do is great and I don't mean to sound like a prick, but I'm wondering if you can shed some light on what happened and why. Thanks.

comment:9 Changed 13 years ago by Scott González

Resolution: fixed
Status: reopenedclosed

Please use the forums for discussions.

comment:10 Changed 13 years ago by be.davestein

Ironic since I started there and headed here since I found this as the source of problem :) Here is the post. Sorry about that... http://forum.jquery.com/topic/jqueryui-1-8-5

comment:11 Changed 13 years ago by Scott González

Milestone: 1.8.51.9
Resolution: fixed
Status: closedreopened

Reverted in 6ba75aa. Going to add back in for 1.9.

comment:12 Changed 13 years ago by Scott González

Resolution: fixed
Status: reopenedclosed

Widget: Throw errors when calling non-existent methods or methods on uninistantiated widgets. Fixes #5972 - Widget: Throw error for non-existent method calls.

Changeset: 1e28040cf358d0fe81ee83a2e35d7dbb65362afa

comment:13 Changed 13 years ago by Scott González

Resolution: fixed
Status: closedreopened

comment:14 Changed 13 years ago by Scott González

Resolution: fixed
Status: reopenedclosed

Widget: Throw errors for invalid method calls. Fixes #5972 - Widget: Throw error for non-existent method calls.

Changeset: 9ad2a4b1ccebb32cc745be3ef85a4b634e416ff8

Note: See TracTickets for help on using tickets.