Provides the ability to bind and trigger custom named events. Bound callback functions may be triggered asynchronously or synchronously returning results.

Constructors

  • Provides a constructor which optionally takes the eventbus name.

    Parameters

    • Optionalname: string

      Optional eventbus name.

    Returns Eventbus

Accessors

  • get callbackCount(): number
  • Returns the current callback count.

    Returns number

    The current callback count.

  • get eventCount(): number
  • Returns the current event count.

    Returns number

    Returns the current event count.

  • get name(): string
  • Returns the current eventbus name.

    Returns string

    The current eventbus name.

Methods

  • Just like on, but causes the bound callback to fire several times up to the count specified before being removed. When multiple events are passed in using the space separated syntax, the event will fire count times for every event you passed in, not once for a combination of all events.

    Parameters

    • count: number

      Number of times the function will fire before being removed.

    • name: string | EventMap

      Event name(s) or event map.

    • callback: object | Function

      Event callback function or context for event map.

    • Optionalcontext: object

      Event context

    • Optionaloptions: EventOptions | EventOptionsOut

      Event registration options.

    Returns Eventbus

    This Eventbus instance.

  • Returns an iterable for all stored events yielding an array with event name, callback function, and event context.

    Parameters

    • Optionalregex: RegExp

      Optional regular expression to filter event names.

    Returns Generator<[string, Function, object, EventOptionsOut], void, unknown>

    Generator

  • Returns the options of an event name.

    Parameters

    • name: string

      Event name(s) to verify.

    Returns EventOptionsOut

    The event options.

  • Returns the trigger type of event name. Note: if trigger type is not set then undefined is returned for type otherwise 'sync' or 'async' is returned.

    Parameters

    • name: string

      Event name(s) to verify.

    Returns string

    The trigger type.

  • Returns whether an event name is guarded.

    Parameters

    • name: string | EventMap

      Event name(s) or event map to verify.

    • Optionaldata: object

      Stores the output of which names are guarded.

    Returns boolean

    Whether the given event name is guarded.

  • Returns an iterable for the event names / keys of registered event listeners.

    Parameters

    • Optionalregex: RegExp

      Optional regular expression to filter event names.

    Returns Generator<string, void, unknown>

  • Returns an iterable for the event names / keys of registered event listeners along with event options.

    Parameters

    • Optionalregex: RegExp

      Optional regular expression to filter event names.

    Returns Generator<[string, EventOptionsOut], void, unknown>

    Generator

  • Tell an object to listen to a particular event on another object. The advantage of using this form, instead of other.on(event, callback, object), is that listenTo allows the object to keep track of the events, and they can be removed all at once later on. The callback will always be called with object as context.

    Parameters

    • obj: object

      Event context

    • name: string | EventMap

      Event name(s) or event map.

    • callback: object | Function

      Event callback function or context for event map.

    Returns Eventbus

    This Eventbus instance.

    view.listenTo(model, 'change', view.render);
    
  • Just like listenTo, but causes the bound callback to fire count times before being removed.

    Parameters

    • count: number

      Number of times the function will fire before being removed.

    • obj: object

      Target event context.

    • name: string | EventMap

      Event name(s) or event map.

    • callback: object | Function

      Event callback function or context for event map.

    Returns Eventbus

    This Eventbus instance.

  • Just like listenTo, but causes the bound callback to fire only once before being removed.

    Parameters

    • obj: object

      Target event context

    • name: string | EventMap

      Event name(s) or event map.

    • callback: object | Function

      Event callback function or context for event map.

    Returns Eventbus

    This Eventbus instance.

  • Remove a previously-bound callback function from an object. If no context is specified, all the versions of the callback with different contexts will be removed. If no callback is specified, all callbacks for the event will be removed. If no event is specified, callbacks for all events will be removed.

    Note that calling model.off(), for example, will indeed remove all events on the model.

    Parameters

    • Optionalname: string | EventMap

      Event name(s) or event map.

    • Optionalcallback: Function

      Event callback function

    • Optionalcontext: object

      Event context

    Returns Eventbus

    This Eventbus instance.

    // Removes just the `onChange` callback.
    object.off('change', onChange);

    // Removes all 'change' callbacks.
    object.off('change');

    // Removes the `onChange` callback for all events.
    object.off(null, onChange);

    // Removes all callbacks for `context` for all events.
    object.off(null, null, context);

    // Removes all callbacks on `object`.
    object.off();
  • Bind a callback function to an object. The callback will be invoked whenever the event is fired. If you have a large number of different events on a page, the convention is to use colons to namespace them: 'poll:start', or 'change:selection'.

    To supply a context value for this when the callback is invoked, pass the optional last argument: model.on('change', this.render, this) or model.on({change: this.render}, this).

    Parameters

    • name: string | EventMap

      Event name(s) or event map.

    • callback: object | Function

      Event callback function or context for event map.

    • Optionalcontext: object

      Event context

    • Optionaloptions: EventOptions | EventOptionsOut

      Event registration options.

    Returns Eventbus

    This Eventbus instance.

    // The event string may also be a space-delimited list of several events...
    book.on('change:title change:author', ...);
    Callbacks bound to the special 'all' event will be triggered when any event occurs, and are passed the name of
    the event as the first argument. For example, to proxy all events from one object to another:
    proxy.on('all', (eventName) => {
    object.trigger(eventName);
    });
    All event methods also support an event map syntax, as an alternative to positional arguments:
    book.on({
    'change:author': authorPane.update,
    'change:title change:subtitle': titleView.update,
    'destroy': bookView.remove
    });
  • Just like on, but causes the bound callback to fire only once before being removed. Handy for saying "the next time that X happens, do this". When multiple events are passed in using the space separated syntax, the event will fire once for every event you passed in, not once for a combination of all events

    Parameters

    • name: string | EventMap

      Event name(s) or event map.

    • callback: object | Function

      Event callback function or context for event map.

    • Optionalcontext: object

      Event context.

    • Optionaloptions: EventOptions | EventOptionsOut

      Event registration options.

    Returns Eventbus

    This Eventbus instance.

  • Tell an object to stop listening to events. Either call stopListening with no arguments to have the object remove all of its registered callbacks ... or be more precise by telling it to remove just the events it's listening to on a specific object, or a specific event, or just a specific callback.

    Parameters

    • obj: object

      Event context

    • Optionalname: string

      Event name(s)

    • Optionalcallback: Function

      Event callback function

    Returns Eventbus

    This Eventbus instance.

    view.stopListening();

    view.stopListening(model);
  • Trigger callbacks for the given event, or space-delimited list of events. Subsequent arguments to trigger will be passed along to the event callbacks.

    Parameters

    • name: string

      Event name(s)

    • Rest...args: any[]

      Additional arguments passed to the event function(s).

    Returns Eventbus

    This Eventbus instance.

  • Provides trigger functionality, but collects any returned Promises from invoked targets and returns a single Promise generated by Promise.resolve for a single value or Promise.all for multiple results. This is a very useful mechanism to invoke asynchronous operations over an eventbus.

    Parameters

    • name: string

      Event name(s)

    • Rest...args: any[]

      Additional arguments passed to the event function(s).

    Returns Promise<any>

    A Promise with any results.

  • Defers invoking trigger. This is useful for triggering events in the next clock tick.

    Parameters

    • name: string

      Event name(s)

    • Rest...args: any[]

      Additional arguments passed to the event function(s).

    Returns Eventbus

    This Eventbus instance.

  • Provides trigger functionality, but collects any returned result or results from invoked targets as a single value or in an array and passes it back to the callee in a synchronous manner.

    Parameters

    • name: string

      Event name(s).

    • Rest...args: any[]

      Additional arguments passed to the event function(s).

    Returns any

    The results of the event invocation.

Properties

_listeners: {}
_listenId: string
_listeningTo: {}