Skip to main content

Bug Tracker

Side navigation

#3699 closed enhancement (fixed)

Opened December 08, 2008 05:00PM UTC

Closed May 07, 2009 01:53AM UTC

Allow closure-less defining of scope for events

Reported by: natecavanaugh Owned by: brandon
Priority: major Milestone: 1.4
Component: event Version: 1.2.6
Keywords: Cc: natecavanaugh
Blocked by: Blocking:
Description

An issue that we're running into is that IE in particular uses quite a bit of memory when using closures to maintain object scope to use a method for an event on an element.

The use case is that it is often much better to organize code into objects and often the element itself is only a small part of the total work being done with an event.

Here is how I would propose the API (which is similar to the bindo plugin, however, that plugin uses closures, which suffer the same memory issues):

jQueryObject.bind('click', [obj, fn], data);

Basically, if the second parameter is an array, check if it contains an object to set the event scope to, and the second element of the array can be either the function itself or a string name.

So both of these would work:

jQueryObject.bind('click', [MyObject, MyObject.method])

jQueryObject.bind('click', [MyObject, 'method'])

Allowing this would greatly enhance larger applications to be able to maintain an object scope, and prevent anonymous functions from keeping a large set of variables in memory when they're not needed.

Thanks in advance,

Attachments (1)
  • event_scope.diff (4.6 KB) - added by brandon May 04, 2009 04:40AM UTC.

    event scope patch

Change History (5)

Changed December 11, 2008 12:35PM UTC by flesler comment:1

cc: → natecavanaugh
component: unfilledevent
owner: fleslerbrandon

I doubt we'll include this into the core, but it's a good functionality for a plugin.

Still... I'll leave this opened for now.

Changed December 17, 2008 05:19PM UTC by natecavanaug comment:2

Honestly, it's a horrible functionality for a plugin.

A plugin would have to completely overwrite fairly large pieces of the event portion just to get a few lines changed.

This is not only unwise for a plugin developer to do (considering the internal API could change), but it's also unnecessary code that the client will have to re-download and re-execute.

This really should be added to the core, and if not, I would really love to understand why. Closures can be very powerful, but 70+% of the market is still running the JScript engine which handles closures pretty darn poorly.

Also, I believe every major Javascript framework allows this (with the exception of Prototype, which uses a closure in their bindAsEventListener method).

Lastly, from what I could perceive, and I could be wrong, but the API change would not cause problems for people who want to use it their normal way.

I would respectfully ask that you guys reconsider adding this to the core.

Thanks,

Changed December 17, 2008 06:17PM UTC by paul comment:3

Hey Brandon, I have to agree with Nate here. It's a trivial change in the core, and I would certainly make use of it in many instances, and a horrible effort in a plugin.

Actually, I would probably use the syntax at least 2-3 times on each jQuery UI widget.

Changed May 04, 2009 04:39AM UTC by brandon comment:4

milestone: 1.31.3.3

I don't care much for the proposed syntax. Instead I'd recommend the following:

$(...).bind("type", fn, scope);
$(...).bind("type", data, fn, scope);

Same signature would apply to .one() and .live().

Changed May 07, 2009 01:53AM UTC by brandon comment:5

resolution: → fixed
status: newclosed

Applied in r6344