arvgta / ajaxify

Ajaxify - The Ajax Plugin
https://4nf.org/
274 stars 124 forks source link

Plain vanilla triggering of custom events #215

Closed arvgta closed 2 years ago

arvgta commented 2 years ago

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
}


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 2 years ago

Will leave it as it is for the moment, in order not to break backward compatilibity...