Re: EventTarget.on

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 UTC