Mixin: bff/event-listener

bff/event-listener

A mixin, providing event listening capabilities to an object. This is an inversion-of-control with regards to regular event listening; the listener maintains a list of the events it is listening to. This allows the listener to remove some or all its event listeners, for instance when it is disabled or destroyed. This is an easy way to avoid leaking listeners. Caveat: don't mix eventEmitter.removeEventListener and eventListener.stopListening throughout a project, as that could result in memory leaks.

Source:

Methods


listenTo(eventEmitters, eventNames, callback [, context])

Start listening to an event on a specified event emitting object. Both eventEmitters and eventNames arguments can be arrays. The total amount of listeners added will be the Cartesian product of the two lists.

Parameters:
Name Type Argument Description
eventEmitters Object | Array | NodeList

One or more event emitters that will be listened to.

eventNames string | Array

One or more string identifiers for events that will be listented to.

callback function

The function that will be called when the event is emitted.

context any <optional>

The context with which the callback will be called (i.e. what "this" will be).
Will default to the caller of .listenTo, if not provided.

Source:

stopListening( [eventEmitter] [, eventName])

Stop listening to events. If no arguments are provided, the listener removes all its event listeners. Providing any or both of the optional arguments will filter the list of event listeners removed.

Parameters:
Name Type Argument Description
eventEmitter Object <optional>

If provided, only callbacks attached to the given event emitter will be removed.

eventName string <optional>

If provided, only callbacks attached to the given event name will be removed.

Source: