EventBroker is a static publish and subscribe class I wrote that you can use to dispatch and receive events. The listener does not know who dispatched the event but both of them need to know the event type.
EventBroker.dispatchEvent('change');
var data = {some: 'data'}
EventBroker.dispatchEvent('change', data);
EventBroker.addEventListener('change', this.handlerMethod, this);
ClassName.prototype.enable = function () {
EventBroker.addEventListener('change', this.handlerMethod, this);
};
// The event passed to the method will always be a BaseEvent object.
ClassName.prototype.handlerMethod = function (baseEvent) {
console.log(baseEvent.type, baseEvent.data);
}
Your handler method will always receive a BaseEvent object.
EventBroker.removeEventListener('change', this.handlerMethod, this);
EventBroker.hasEventListener('change', this.handlerMethod, this);
// Returns true or false
EventBroker.getEventListeners();
// [ClassName] is listen for 'BaseEvent.change' event.
Checkout the full docs for EventBroker.
The EventBroker class is a module of a JavaScript/TypeScript library called StructureJS. Take a look at it here: StructureJS.