Simple JavaScript class for managing events in JavaScript
Installation
In a browser
Download eventbus.min.js and add it to your page.
In Node
npm i eventbusjs -S
And then in your code:
var EventBus = ;
API
addEventListener
// @type - string// @callback - function// @scope - the scope where the @callback is definedEventBus
removeEventListener
// @type - string// @callback - function// @scope - the scope where the @callback is definedEventBus
hasEventListener
// @type - string// @callback - function// @scope - the scope where the @callback is definedEventBus
dispatch
// @type - string// @target - the caller// @args - pass as many arguments as you wantEventBus
getEvents
For debugging purpose, it prints out the added listeners.
EventBus
Usage
{ console;}EventBus;EventBus;
Keeping the scope
var { thisclassName = "TestClass1"; this { console; }};var { thisclassName = "TestClass2"; this { EventBus; }};var t1 = ;var t2 = ;EventBus;t2;
Passing additional parameters
var { thisclassName = "TestClass1"; this { console; console; console; console; }};var { thisclassName = "TestClass2"; this { EventBus; }}; var t1 = ;var t2 = ; EventBus;t2;
Example of usage EventBus.removeEventListener
To remove EventListener you have to pass same instance of callback
/* Wrong - callback functions are different instances */EventBus;EventBus; /* Correct - callback function is the same instance */var { console;};EventBus;EventBus;