Kevin 08b0754135 grid programme | hace 4 años | |
---|---|---|
.. | ||
sandbox | hace 4 años | |
test | hace 4 años | |
README.md | hace 4 años | |
bower.json | hace 4 años | |
ev-emitter.js | hace 4 años | |
package.json | hace 4 años |
Lil' event emitter — add a little pub/sub
EvEmitter adds publish/subscribe pattern to a browser class. It's a smaller version of Olical/EventEmitter. That EventEmitter is full featured, widely used, and great. This EvEmitter has just the base event functionality to power the event API in libraries like Isotope, Flickity, Masonry, and imagesLoaded.
// Inherit prototype, IE8+
MyClass.prototype = new EvEmitter();
// Inherit prototype, IE9+
MyClass.prototype = Object.create( EvEmitter.prototype );
// Mixin prototype
_.extend( MyClass.prototype, EvEmitter.prototype );
// single instance
var emitter = new EvEmitter();
Add an event listener.
emitter.on( eventName, listener )
eventName
- String - name of the eventlistener
- FunctionRemove an event listener.
emitter.off( eventName, listener )
Add an event listener to be triggered only once.
emitter.once( eventName, listener )
Trigger an event.
emitter.emitEvent( eventName, args )
eventName
- String - name of the eventargs
- Array - arguments passed to listenersRemoves all event listeners.
emitter.allOff()
// create event emitter
var emitter = new EventEmitter();
// listeners
function hey( a, b, c ) {
console.log( 'Hey', a, b, c )
}
function ho( a, b, c ) {
console.log( 'Ho', a, b, c )
}
function letsGo( a, b, c ) {
console.log( 'Lets go', a, b, c )
}
// bind listeners
emitter.on( 'rock', hey )
emitter.once( 'rock', ho )
// trigger letsGo once
emitter.on( 'rock', letsGo )
// emit event
emitter.emitEvent( 'rock', [ 1, 2, 3 ] )
// => 'Hey', 1, 2, 3
// => 'Ho', 1, 2, 3
// => 'Lets go', 1, 2, 3
// unbind
emitter.off( 'rock', ho )
emitter.emitEvent( 'rock', [ 4, 5, 6 ] )
// => 'Hey' 4, 5, 6
EvEmitter is released under the MIT License. Have at it.