Re: [whatwg/dom] Event delegation via EventListenerOptions (#215)

@FremyCompany It’s better because the proposed matching is not on `event.target` but on `event.target` and its ancestors up to the element the listener is attached to.

It’s a great question though, because it shows that this option should not be called `matches:sel` if it doesn’t do something close to a `if(!event.target.matches(sel))return;` at the start of your callback.

Tossing a coin in the bikeshed machine:

> A new matches option (or delegate option? delegateSelector? filter? #bikeshedtime) that takes a selector

I vote "anything but `matches` or `closest`".
`delegate` or `delegateSelector` are okay-ish, but if you want `event.delegateTarget` to be the element the event listener is attached to then it’s a contradiction:

```js
document.body // event.delegateTarget
  .addEventListener('x', event => { … }, {
    delegateSelector: '.foo' // event.currentTarget
  })
```

Possible solutions when keeping the "delegation" root:
- `delegation: '.foo'`
- `delegated: '.foo'` ("the elements for which we're delegating this event listener will match this selector")
- `delegateFor: '.foo'` or `asDelegateFor: '.foo'` ("the element we're attaching an event listener to is a delegate for descendants matching this selector")

With the "for" root, you can also do:
- `forEach: '.foo'` ("call the callback for each element matching this selector between the event.target and the event.delegateTarget")

My faves might be `delegated` and `filter`, but I have no strong opinion besides "beware of misnomers".

> - event.currentTarget and the this value get redefined/changed when delegation is involved, to be "the object on event's propagation path for which callbacks are currently being invoked", which is apparently a generalization of the current definition.
> - A new event.delegateTarget property which is defined the way event.currentTarget is currently defined

This seems fine. It follows the precedent set by jQuery but also adds the ability to retrieve the element the event listener is attached to (delegateTarget).

One could argue that the jQuery way was "bad" it would be cleaner to not redefine `event.currentTarget` and instead add a new property for the actual target (`event.delegatedTarget` maybe). But personnally, I’m okay with not having to relearn "use event.currentTarget in 99% of cases". :P

-- 
You are receiving this because you are subscribed to this thread.
Reply to this email directly or view it on GitHub:
https://github.com/whatwg/dom/issues/215#issuecomment-310836557

Received on Saturday, 24 June 2017 12:47:10 UTC