arvgta / ajaxify

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

ES6 event delegation #206

Closed arvgta closed 3 years ago

arvgta commented 3 years ago

Sources of research so far:


Can you think of a variant of the below that preservese.currentTarget?

function _on(eventName, elementSelector, handler, el = document) { //e.currentTarget is document when the handler is called
    el.addEventListener(eventName, function(e) {
        // loop parent nodes from the target to the delegation node
        for (var target = e.target; target && target != this; target = target.parentNode) {
            if (target.matches(elementSelector)) {
                handler(target, e);
                break;
            }
        }
    }, false);
}

It is important to note, that e.currentTarget is not available as usual. (I had to patch that)

I also substituted

with