Class: Evented

barejs.Evented()

Evented is a base class that adds Eventing to JavaScript Objects. Extends Destroyable to automatically remove listeners if the object is destroyed. If handles given to the listener are owned (or manually destroyed at the appropiate time), the event link between Evented and its listener will be removed as soon as either party is destroyed.

Constructor

new Evented()

The evented constructor will invoke the Destroyable constructor to ensure the object is initialized correctly.

Source:

Extends

Classes

EventedHandle

Methods

addDestroyListener(_listener) → {function}

Register a destroy listener for this Destroyable object.

Parameters:
Name Type Description
_listener module:barejs.Destroyable~DestroyListener

The listener function to add.

Inherited From:
Source:
Returns:

the listener

Type
function

destroy()

Destroy the Evented object. This will clean up the object, removing any links to listeners.

Overrides:
Source:

destroyAll(_collection)

Utility method that will iterate a collection and destroy all items in it.

Parameters:
Name Type Description
_collection object

An object with a forEach method or length property (e.g. an Array).

Inherited From:
Source:

emit(_eventName, _eventArgs) → {module:barejs.EventArgs}

Emit the event with _eventName.

Parameters:
Name Type Description
_eventName string

The name of the event to emit.

_eventArgs module:barejs.EventArgs

the event args to emit.

Source:
Returns:

_eventArgs (or null if not specified).

Type
module:barejs.EventArgs

hasListener(_eventName) → {Boolean}

Check if there is at least one listener for _eventName. It is highly recommended to just emit an event instead of checking if there are listeners. This method is provided for edge cases where building the event metadata is an expensive process, which should be avoided if there are no listeners.

Parameters:
Name Type Description
_eventName string

The name of the event to check.

Source:
Returns:

True if there is at least one listener, false otherwise.

Type
Boolean

on(_eventName, _listener) → {module:barejs.Evented~EventedHandle}

Listen to an event. To stop listening, call destroy the returned handle. Listeners should own the handle returned by this method, so it is automatically disconnected when the listener is destroyed.

Parameters:
Name Type Description
_eventName string

The event to listen to.

_listener module:barejs.Evented~EventListener

The callback to call if the event occurs.

Source:
Returns:
Type
module:barejs.Evented~EventedHandle

once(_eventName, _listener) → {module:barejs.Evented~EventedHandle}

Listen for an event, automatically detaching after one invocation of the listener.

Parameters:
Name Type Description
_eventName string

The event to listen to.

_listener module:barejs.Evented~EventListener

The callback to call (once) if the event occurs.

Source:
Returns:
Type
module:barejs.Evented~EventedHandle

own() → {Array}

Own a number of handles. Returns an array of the owned handles.

Inherited From:
Source:
Returns:

The owned handles.

Type
Array

ownMember(_name, _target)

The ownMember function combines ref and own into 1 call. The target is owned and then ref-ed as _name.

Parameters:
Name Type Description
_name string

The name of the member.

_target module:barejs.Destroyable

The target to own.

Inherited From:
Source:
Returns:

The owned _target

ref(_name, _target)

Reference a target as a member property that will be unlinked on destroy. If the referenced target is also Destroyable, the ref is also cleared if the target is destroyed.

Parameters:
Name Type Description
_name string

The name to reference.

_target object

The object to assign to the reference.

Inherited From:
Source:
Returns:

The value of this[_name].

removeDestroyListener(_listener)

Unregister a destroy listener for this Destroyable object.

Parameters:
Name Type Description
_listener module:barejs.Destroyable~DestroyListener

The listener function to remove.

Inherited From:
Source:

unref(_name, _valueopt)

Remove a reference (by name). If the name was given to ownMember, the member is NOT removed from the list of owned targets. Does NOT destroy the value currently referenced.

Parameters:
Name Type Attributes Description
_name string

the name to remove the reference to

_value object | function <optional>

If a value is provided (and is not null), unref will only clear the reference if _value equals whatever is currently ref-ed.

Inherited From:
Source:

Type Definitions

EventListener(_eventArgs, _sender)

Event listeners are called with two arguments; the EventArgs and the sender.

Parameters:
Name Type Description
_eventArgs module:barejs.EventArgs

The EventArgs to the event.

_sender module:barejs.Evented

The Evented object that emitted the event.

Source: