Plain vanilla triggering of custom events
Closed this issue · 1 comments
arvgta commented
EDIT: Not necessary to adopt this approach because there were no timing problems after all...
Question: Are the following two approaches correct equivalents?
The current native approach(suspected to be buggy):
_trigger = (t, e) => {
let ev = document.createEvent('HTMLEvents');
ev.initEvent("pronto." + t, true, false);
ev.data = e ? e : RQ("e");
window.dispatchEvent(ev);
document.dispatchEvent(ev);
}
At first sight, I would suspect, that at the very least the second dispatchEvent()
in the above approach is superfluous and maybe even results in double firing?
The old jQuery approach(definitely works):
function _trigger(t, e){ //trigger a custom event
e = e ? e : RQ("e"); //if e not passed, then use existing internal event data
jQuery(window).trigger("pronto." + t, e); //always prepend "pronto." and pass the bespoke data in the second variable
}
- Here is how the translation is proposed at YouMightNotNeedjQuery
- Here is how the translation is proposed at developer.mozilla.org
That's to say, that the following should work on everything except IE ->
_trigger = (t, e) => {
window.dispatchEvent(
new CustomEvent("pronto." + t, { detail: { e ? e : RQ("e") } })
)
}
What do you think of the latter approach?
arvgta commented
Will leave it as it is for the moment, in order not to break backward compatilibity...