Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Event not triggered if using delegation #236

Closed
netfusion opened this issue Nov 14, 2018 · 5 comments
Closed

Event not triggered if using delegation #236

netfusion opened this issue Nov 14, 2018 · 5 comments

Comments

@netfusion
Copy link

Some events do not fire properly when delegation is used.
Examples are mouseenter and mouseleave but there may be more.

Test with cash: https://jsfiddle.net/dyo9xcq0/
Test with jQuery: https://jsfiddle.net/w62utcrj/

(Move the mouse over the elements and watch console output)

@fabiospampinato
Copy link
Owner

fabiospampinato commented Nov 15, 2018

Some events do not bubble, and remember in the case of event delegation we are listening for events on the target element.

So instead of writing:

$("#outer").on("mouseenter", "#inner", function(e) {
  console.log(e.currentTarget);
});

you should be writing:

$("#inner").on("mouseenter", function(e) {
  console.log(e.currentTarget);
});

Of course that takes away the convenience that comes with event delegation if you're adding elements dynamically to the page.

I'm not sure what can be done here, I'll take a look at how jQuery implements this but there's probably nothing that we can do, they are basically rewriting the event system themselves.

@netfusion
Copy link
Author

netfusion commented Nov 15, 2018

A common use case would be a tooltip system which relies on attributes present on any kind of element.

$("body").on("mouseenter", "[data-tooltip-content]", function(e) {
    $("#tooltip").text($(this).data("tooltipContent"));  
});

Im my case I am using an MutationObserver now.

Maybe cash could use a MutationObserver to attach the delegated event handlers to matching newly created DOM elements instead of attaching them to the parent and cycling the DOM. This would solve this issue as well as #235.

@fabiospampinato
Copy link
Owner

Mutation Observers aren't available in IE10. This sounds interesting though, I'll see what can be done 👍

@fabiospampinato
Copy link
Owner

Fixed in 872f79d. I'll publish a new beta as soon as I fix as few other problems.

It turned out that there are some events that do not bubble, and weirdly enough there are alternative events for them that do the same thing except that they bubble 🤷‍♂️

focus -> focusin
blur -> focusout
mouseenter -> mouseover
mouseleave -> mouseout

@fabiospampinato
Copy link
Owner

Btw this code:

$("body").on("mouseenter", "[data-tooltip-content]", function(e) {
    $("#tooltip").text($(this).data("tooltipContent"));  
});

Should have awful performance. mouseenter/mousover che fire tens of times every time you move the mouse. Also we have to traverse up the tree until we reach the body to check if any of those elements match [data-tooltip-content], which is on its own a slow selector.

You should instead find all those elements once, and maybe listen for the addition of other elements to the dom that match that selector (like you seem to be doing now?).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants