Why is 'el' added to event data when using the 'this.on' delegate pattern?
necolas opened this issue · comments
The delegate pattern for this.on
adds el
to the data:
this.on('eventName', {
someSelector: f
});
this.f = function (e, data) {
console.log(data.el);
// => DOMNode
};
Can't work out why. Thanks
We've been making use of this - it's a nice, easy reference to the node
which matched the delegate selector. Useful for looking at attributes on
el. IIRC, otherwise you have to do a $(event.target).closest(selector), or
something similar.
On 27 July 2014 22:19, Nicolas Gallagher notifications@github.com wrote:
The delegate pattern for this.on adds el to the data:
this.on('eventName', {
someSelector: f});
this.f = function (e, data) {
console.log(data.el);
// => DOMNode};Can't work out why. Thanks
—
Reply to this email directly or view it on GitHub
#290.
We kind of discussed this once https://github.com/flightjs/flight/pull/199/files
However it is still there. Also I am no 100% sure that what @angus-c said is correct (have to test it)
e.delegateTarget
would be document
while e.currentTarget
should be the correct one?
@giuseppeg neither will be the element you're after afaik. Question is about whether we can instead augment the jquery event
with a new property (e.g., e.delegator
), since it's the object that has all the other node references, rather than polluting the event payload. With this Flight pattern, you can't do a simple iteration over the props of the data in a handler, because you have to filter out el
in case the handler is used in this delegation pattern.
@necolas In the pattern you are using it for I would create a props
property to iterate over. Better to call it out explicitly than rely on data having the right thing (e.g., what if later you wanted to add another payload item that was not to be iterated over
So
Object.keys(data.props).forEach(//..
not
Object.keys(data).forEach(//..
I like the idea of adding a property to the Event object rather than data. Adding it to event data goes against own own rule of data being serializable.