[whatwg] Exposing EventTarget to JavaScript

Alex Russell slightlyoff at google.com
Wed Apr 29 11:39:19 PDT 2009


On Fri, Apr 24, 2009 at 6:37 PM, Boris Zbarsky <bzbarsky at mit.edu> wrote:
> Ojan Vafai wrote:
>>
>> What would be a better approach?
>
> I believe Alex proposed some in this thread as aliases for addEventListener.
>  Those looked a lot better to me, for what it's worth.
>
> If a linear list of things the event is targeting is sufficient, of course,
> and we're ok with the random third argument of addEventListener being carted
> around, then we might be ok using it...
>
> From my point of view, in addition to the things already mentioned, an issue
> with addEventListener is that removing requires a match of both the listener
> and the bubbles arg.  So for example:
>
>  node.addEventListener("foo", function() { ... }, false);
>
> if I want to remove it later, I suddenly have to pull the function out and
> give it a name.  And then on the remove end duplicate the "foo" and false.
> Maybe it's just me, but I'd have much preferred something like:
>
>  var token = node.addEventListener("foo", function() { ... });
>
>  // later on
>  node.removeEventListener(token);
>
> or some such.

FWIW, dojo.connect() provides a nearly identical scheme to work around
this very problem. For example, you can do:

  var handle = dojo.connect(node, "onclick", function(){ ... });
  // or:
  //    var handle = dojo.connect(node, "onclick", object, "method");
  //
  // ... time passes ...
  dojo.disconnect(handle);

This isn't quite satisfying since the case of JS dispatch and using
methods from objects introduces the inevitable functions-aren't-bound
ambiguity in today's JS. We can't, for example, do something like:

  var handle = dojo.lookupHandle(node, "onclick", object, "method");

Since internally we do the "turn methods into bound callers trick" via
dojo.hitch() (aka, es5's Function.prototype.bind()).

A workable system that improves on the current state should *at least*
return disconnectable handles, and hopefully also provide some way to
query join points for connections from both "global" function objects
and scoped function objects. That is to say, it should be possible to
do:

   nodeOrObject.unlisten("onclick", object, "method");

   // or:

   nodeOrObject.unlisten("onclick", object.method.bind());

Regards



More information about the whatwg mailing list