dispatchy
A jquery like event emitter/dispatcher that could be mixed with other objects to provide emitting capabilities ala jQuery Style
Install
npm i --save dispatchy
Usage
dispatchy
implements the same interface of the jquery events related methods, but for any object.
Example
var dispatchy = ; var myObject = dispatchy; // will return a newly fresh dispatchy instance for your use and pleasure // now you can do myObject; later you can myObject; //and you will see in the console// received data.
API
The main methods are:
-
on: add event listeners to the dispatcher instance
var obj = {};var dispatchy = ;var extend = ;;obj;obj;obj;obj; -
off: remove event listeners from the dispatcher instance
var {console;};// registering the listenerobj;// removing itobj;// registering the listenerobj;// removing it using the namespace. No need to pass the listenerobj;// registering the listenersobj;obj;// removing all events with the given namespace. handy to remove all events with the same namespaceobj; -
one: add an event to the dispatcher instance that will be removed after its execution
var obj = {};var dispatchy = ;var extend = ;;obj;obj;obj;obj; -
fire: fires an event, executing all the listeners added to the particular fired event,
var {console;};// registering the listenerobj;// executing itobj;// registering other listenerobj;// this will fire the event with the given namespace if found. won't try to fire some:event.obj;// execute all events of the given namespaceobj;
Other methods: This methods are not like the ones in jQuery but can be used to achieve something like
special events
on the dispatchers.
-
registerEvent: Register an event with a given lifecycle, similar to jQuery Special Events
var customEventLifeCycle =// executed the first time a listener is added for the given event{}// executed every time an event listener is added{}// executed every time an event listener is removed{}// execute when the last listener is about to be removed from the dispatcher{};var dispatchy = ;dispatchy; -
registerPersistentEvent: For a lack of a better name this method register a special event that once is fired Any listeners added after will be immediately executed. Is like the jQuery
ready
event.var dispatchy = ;dispatchy;dispatchy; // no event listeners added yet.. no problem// later in the codedispatchy;