Class: Emitter

stb/emitter~ Emitter

new Emitter()

Base Events Emitter implementation.

Source:
See:

Members

events :Object.<string, Array.<function()>>

Inner hash table for event names and linked callbacks. Manual editing should be avoided.

Type:
  • Object.<string, Array.<function()>>
Source:
Example
{
    click: [
        function click1 () { ... },
        function click2 () { ... }
    ],
    keydown: [
        function () { ... }
    ]
}

Methods

addListener(name, callback)

Bind an event to the given callback function. The same callback function can be added multiple times for the same event name.

Parameters:
Name Type Description
name string

event identifier

callback function

function to call on this event

Source:
Example
var obj = new Emitter();
obj.addListener('click', function ( data ) { ... });
// one more click handler
obj.addListener('click', function ( data ) { ... });

addListeners(callbacks)

Apply multiple listeners at once.

Parameters:
Name Type Description
callbacks Object

event names with callbacks

Source:
Example
var obj = new Emitter();
obj.addListeners({click: function ( data ) {}, close: function ( data ) {}});

emit(name, dataopt)

Execute each of the listeners in the given order with the supplied arguments.

Parameters:
Name Type Attributes Description
name string

event identifier

data Object <optional>

options to send

Source:
To Do:
  • consider use context
Example
obj.emit('init');
obj.emit('click', {src:panel1, dst:panel2});

// it's a good idea to emit event only when there are some listeners
if ( this.events['click'] !== undefined ) {
    this.emit('click', {event: event});
}

once(name, callback)

Add a one time listener for the event. This listener is invoked only the next time the event is fired, after which it is removed.

Parameters:
Name Type Description
name string

event identifier

callback function

function to call on this event

Source:

removeAllListeners(nameopt)

Remove all callbacks for the given event name. Without event name clears all events.

Parameters:
Name Type Attributes Description
name string <optional>

event identifier

Source:
Example
obj.removeAllListeners('click');
obj.removeAllListeners();

removeListener(name, callback)

Remove all instances of the given callback.

Parameters:
Name Type Description
name string

event identifier

callback function

function to remove

Source:
Example
obj.removeListener('click', func1);