Bug Tracker

Ticket #3699 (closed enhancement: fixed)

Opened 6 years ago

Last modified 6 years ago

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
Blocking: Blocked by:

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

event_scope.diff Download (4.6 KB) - added by brandon 6 years ago.
event scope patch

Change History

comment:1 Changed 6 years ago by flesler

  • Cc natecavanaugh added
  • Owner changed from flesler to brandon
  • Component changed from unfilled to event

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.

comment:2 Changed 6 years ago by natecavanaug

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,

comment:3 Changed 6 years ago by paul

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.

comment:4 Changed 6 years ago by brandon

  • Milestone changed from 1.3 to 1.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 6 years ago by brandon

event scope patch

comment:5 Changed 6 years ago by brandon

  • Status changed from new to closed
  • Resolution set to fixed

Applied in r6344

Note: See TracTickets for help on using tickets.