API Docs for: 1.0.0
Show:

EventEmitter Class

Defined in: src\EventEmitter.js:1

Represents an object that can bind event handlers to arbitrary event names and fire events.

Inheriting this class gives a descendant the ability to register and fire arbitrary events. You can inherit this class by using the Function.call method of the EventEmitter constructor in your class's constructor (see the example below).

Constructor

EventEmitter

()

Example:

function Descendant() {
    EventEmitter.call(this);

    this.otherMethod = function () {
        // ...
    };

    // other methods and properties
}

var object = new Descendant();

object.on('salute', function () {
    alert('Hello, world!');
});

object.emit('salute');

Item Index

Methods

bind

(
  • name
  • handler
  • [scope]
)
chainable

Binds the specified event handler to the specified event name.

The event name can contain any characters.

Parameters:

  • name String

    The name of the event.

  • handler Function

    The event handler.

  • [scope] Object optional

    An optional object used as this when the handler is invoked.

emit

(
  • name
  • [arguments]
)
chainable

An alias for trigger.

Parameters:

  • name String

    The name of the event to fire.

  • [arguments] Any optional multiple

    Zero or more arguments to forward to the handlers.

off

(
  • name
  • handler
)
chainable

An alias for unbind.

Parameters:

  • name String

    The name of the event.

  • handler Function

    The previously bound event handler. It must be exactly the same function reference used in the bind or on call.

on

(
  • name
  • handler
  • [scope]
)
chainable

An alias for bind.

Parameters:

  • name String

    The name of the event.

  • handler Function

    The event handler.

  • [scope] Object optional

    An optional object used as this when the handler is invoked.

once

(
  • name
  • handler
  • [scope]
)
chainable

Binds the specified event handler to the specified event name.

The event name can contain any characters.

The difference with on is that handlers registered with this method are executed at most once and then automatically unbound.

Parameters:

  • name String

    The name of the event.

  • handler Function

    The event handler.

  • [scope] Object optional

    An optional object used as this when the handler is invoked.

register

(
  • name
)
chainable

Adds a name method to this object. The new method can bind or trigger the name event.

The new method is chainable and has two signatures, corresponding to the bind/on and trigger/emit methods.

Parameters:

  • name Object

    String The name of the new method and event to handle.

Example:

emitter.register('salute');

// binds a handler to the 'salute' event
emitter.salute(function () {
    alert('Hello, world!');
});

emitter.salute(); // triggers the 'salute' event

trigger

(
  • name
  • [arguments]
)
chainable

Fires the specified event.

You can optionally pass any number of arguments that will be forwarded to the handlers.

Parameters:

  • name String

    The name of the event to fire.

  • [arguments] Any optional multiple

    Zero or more arguments to forward to the handlers.

unbind

(
  • name
  • handler
)
chainable

Unbinds a previously bound event handler from the specified event name.

Parameters:

  • name String

    The name of the event.

  • handler Function

    The previously bound event handler. It must be exactly the same function reference used in the bind or on call.