W3C home > Mailing lists > Public > www-dom@w3.org > October to December 2011

Re: EventTarget.on

From: Jake Verbaten <raynos2@gmail.com>
Date: Wed, 23 Nov 2011 13:23:03 +0000
Message-ID: <CAMCMjp0=ThA+HpXxsbvg+X23c9Yx06RyLESwO6sHc5Ur0XyL-A@mail.gmail.com>
To: Anne van Kesteren <annevk@opera.com>
Cc: www-dom@w3.org
jQuery's event delegation would mean you could so something like

  eventTarget.on("event", "selector", handler);

which would translate into

eventTarget.addEventListener("event", function _proxy(ev) {
  if (ev.target.matchesSelector("selector") && eventTarget ===
ev.currentTarget) {
    handler.apply(this, arguments);
  }
});

The idea being is that you attach an event to a target but the handler will
only fire if the event bubbled upto the eventTarget and the event target
matches a certain selector.

An example would be

form.on("change", "input", reValidate);

The main advantage over adding an event handler to all input descendants of
the form directly is that the matchesSelector check is done at run time. So
any new input added to the form will automatically be catched by the
delegated event

This would be a useful feature.

On Wed, Nov 23, 2011 at 11:40 AM, Anne van Kesteren <annevk@opera.com>wrote:

> On Mon, 21 Nov 2011 16:13:02 +0100, Jake Verbaten <raynos2@gmail.com>
> wrote:
>
>> addEventListener is long and verbose. Could we have `on` as a synonym for
>> addEventListener ?
>>
>
> I think it should have additional functionality if we are going to add
> something new. E.g. ignore whether the event was dispatched as a bubbling
> event or not and have it always go through the bubble phase. Maybe add the
> possibility of only invoking the listener when it is registered on the
> actual event target, things like that.
>
> Native support for jQuery-style event delegation was suggested during
> TPAC. I'm not that familiar with event delegation, or how jQuery's API
> works here, but I'm not opposed to adding new features around event
> handling.
>
>
> --
> Anne van Kesteren
> http://annevankesteren.nl/
>
Received on Wednesday, 23 November 2011 13:23:35 GMT

This archive was generated by hypermail 2.2.0+W3C-0.50 : Friday, 22 June 2012 06:14:08 GMT