-
Notifications
You must be signed in to change notification settings - Fork 101
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
$.clone doesn't clone delegated events #73
Comments
I believe it works if you do |
@guoguo12 You're right about the glitch with the text node, but the event still doesn't fire. var element = $.create("div", { class: "parent", contents: "This is the parent" });
element._.delegate("click", "p", function() {
done();
});
var child = $.create("p", {});
element.appendChild(child);
var clone = $.clone(element);
var ev = document.createEvent("MouseEvent");
ev.initMouseEvent("click", true, true, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null);
clone.childNodes[1].dispatchEvent(ev); And either I'm really blind today, or it won't even work using addEventListener on the clone when bliss is used: var parent = document.createElement("div");
var child = document.createElement("span");
parent.appendChild(child);
var clone = parent.cloneNode(true);
clone.addEventListener("click", function() {
done();
});
var ev = document.createEvent("MouseEvent");
ev.initMouseEvent("click", true, true, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null);
clone.querySelector("span").dispatchEvent(ev); Nota bene: This JSFiddle demonstrates that this code should be okay. |
@AVGP: Hmm, interesting. Are you saying it only fails during the Karma test? |
Yes. It's weird, isn't it? Here's yet another sample of what works: <!doctype html>
<html>
<body>
<div id="test">
<p>Click me</p>
</div>
<script src="bliss.min.js"></script>
<script>
var original = document.getElementById("test");
original._.delegate("click", "p", function() {
alert("OK");
})
var clone = $.clone(original);
var ev = document.createEvent("MouseEvent");
ev.initMouseEvent("click", true, true, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null);
clone.querySelector("p").dispatchEvent(ev);
</script>
</body>
</html> |
Are you sure that's the only |
@zdfs I don't see how that matters here? |
I see it now. Sorry. I was worried for a second that maybe the delegated event wasn't actually being called. I need more coffee. |
No worries, I just wanted to make sure I'm not missing the elephant in the room ;-) |
What about adding actual contents to the p tag. In my local testing, this seems to work. |
This: var element = $.create("div", { class: "parent", contents: "This is the parent" });
element._.delegate("click", "p", function() {
done();
});
var child = $.create("p", { contents: 'test' });
element.appendChild(child);
var clone = $.clone(element);
var ev = document.createEvent("MouseEvent");
ev.initMouseEvent("click", true, true, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null);
clone.childNodes[1].dispatchEvent(ev); |
@zdfs Yes, that, too, works in a normal page but not in Karma... what the heck? |
Weird. It's working in Karma for me. |
You're passing the |
Probably unrelated, but might make your life easier when dealing with DOM items. https://github.com/LeaVerou/bliss/blob/gh-pages/tests/CoreSpec.js#L4-L14 |
@AVGP - Is this still relevant? I just merged your PR. |
@zdfs unfortunately yes. I'll try later if HTML fixtures work or if it's just my local Karma + Chrome Beta combination that behaves weird... |
Regarding the
|
Agreed. It's currently way too confusing.
|
Agreed. |
Done. :) |
Apart from the |
This test fails but should not:
The text was updated successfully, but these errors were encountered: