Last active
August 11, 2023 07:08
-
-
Save yairEO/cb60592476a4204b27e83048949dbb45 to your computer and use it in GitHub Desktop.
Event Handler Namespace in Vanilla JavaScript
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var events = (function(){ | |
function addRemove(op, events, cb){ | |
if( cb ) | |
events.split(' ').forEach(name => { | |
var ev = name.split('.')[0] | |
cb = cb || this._eventsNS[name] | |
this[op + 'EventListener'].call(this, ev, cb) | |
if(op == 'add') | |
this._eventsNS[name] = cb | |
else | |
delete this._eventsNS[name] | |
}) | |
} | |
function on(events, cb){ | |
this._eventsNS = this._eventsNS || {} // save the _eventsNS on the DOM element itself | |
addRemove.call(this, 'add', events, cb) | |
return this | |
} | |
function off(events, cb){ | |
addRemove.call(this, 'remove', events, cb) | |
return this | |
} | |
// need to work on this method, it is the hardest. | |
// currently I do not know how to dispatch real events (with namespace) like UIEvent, KeyboardEvent, PointerEvent and so no | |
function trigger(eventName, data){ | |
var e; | |
// Don't do events on text and comment nodes | |
if( elem.nodeType === 3 || elem.nodeType === 8 || !eventName ) return | |
try { | |
e = new CustomEvent(eventName) | |
} | |
catch(err){ console.warn(err) } | |
this.dispatchEvent(e) | |
} | |
return { on, off, trigger } | |
})() | |
// Extend the DOM with these above custom methods | |
window.on = document.on = Element.prototype.on = events.on; | |
window.off = document.off = Element.prototype.off = events.off; | |
window.trigger = document.trigger = Element.prototype.trigger = events.trigger; |
- you know
off()
as presented in the usecase cannot work as you are not passing anycb
thusaddRemove()
method won't do anything? - calling
off()
beforeon()
breaks the internet :)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thank you. I'll use this source code well.