Collection

Api-class icon class

Collections are ordered sets of objects. Items in the collection can be retrieved by their indexes in the collection (like in an array) or by their ids.

If an object without an id property is being added to the collection, the id property will be generated automatically. Note that the automatically generated id is unique only within this single collection instance.

By default an item in the collection is identified by its id property. The name of the identifier can be configured through the constructor of the collection.

Type parameters

  • Chevron-right icon

    T : extends Record<string, any>

    The type of the collection element.

Properties

Methods

  • Chevron-right icon

    constructor( initialItems, [ options ] = { [options.idProperty] } )

    Creates a new Collection instance with specified initial items.

    const collection = new Collection<{ id: string }>( [ { id: 'John' }, { id: 'Mike' } ] );
    
    console.log( collection.get( 0 ) ); // -> { id: 'John' }
    console.log( collection.get( 1 ) ); // -> { id: 'Mike' }
    console.log( collection.get( 'Mike' ) ); // -> { id: 'Mike' }
    
    Copy code

    You can always pass a configuration object as the last argument of the constructor:

    const collection = new Collection<{ id: string }>( [ { id: 'John' }, { id: 'Mike' } ] );
    
    console.log( collection.get( 0 ) ); // -> { id: 'John' }
    console.log( collection.get( 1 ) ); // -> { id: 'Mike' }
    console.log( collection.get( 'Mike' ) ); // -> { id: 'Mike' }
    
    Copy code

    Type parameters

    T : extends Record<string, any>

    Parameters

    initialItems : Iterable<T>

    The initial items of the collection.

    [ options ] : object

    The options object.

    Properties
    [ options.idProperty ] : string

    The name of the property which is used to identify an item. Items that do not have such a property will be assigned one when added to the collection.

  • Chevron-right icon

    constructor( [ options ] = { [options.idProperty] } )

    Creates a new Collection instance.

    You can pass a configuration object as the argument of the constructor:

    const emptyCollection = new Collection<{ name: string }>( { idProperty: 'name' } );
    emptyCollection.add( { name: 'John' } );
    console.log( collection.get( 'John' ) ); // -> { name: 'John' }
    
    Copy code

    The collection is empty by default. You can add new items using the add method:

    const emptyCollection = new Collection<{ name: string }>( { idProperty: 'name' } );
    emptyCollection.add( { name: 'John' } );
    console.log( collection.get( 'John' ) ); // -> { name: 'John' }
    
    Copy code

    Type parameters

    T : extends Record<string, any>

    Parameters

    [ options ] : object

    The options object.

    Properties
    [ options.idProperty ] : string

    The name of the property which is used to identify an item. Items that do not have such a property will be assigned one when added to the collection.

  • Chevron-right icon

    Symbol.iterator() → Iterator<T>

    Iterable interface.

    Returns

    Iterator<T>
  • Chevron-right icon

    add( item, [ index ] ) → this

    Adds an item into the collection.

    If the item does not have an id, then it will be automatically generated and set on the item.

    Parameters

    item : T
    [ index ] : number

    The position of the item in the collection. The item is pushed to the collection when index not specified.

    Returns

    this

    Fires

  • Chevron-right icon

    addMany( items, [ index ] ) → this

    Adds multiple items into the collection.

    Any item not containing an id will get an automatically generated one.

    Parameters

    items : Iterable<T>
    [ index ] : number

    The position of the insertion. Items will be appended if no index is specified.

    Returns

    this

    Fires

  • Chevron-right icon

    bindTo( externalCollection ) → CollectionBindToChain<S, T>

    Binds and synchronizes the collection with another one.

    The binding can be a simple factory:

    class FactoryClass {
    	public label: string;
    
    	constructor( data: { label: string } ) {
    		this.label = data.label;
    	}
    }
    
    const source = new Collection<{ label: string }>( { idProperty: 'label' } );
    const target = new Collection<FactoryClass>();
    
    target.bindTo( source ).as( FactoryClass );
    
    source.add( { label: 'foo' } );
    source.add( { label: 'bar' } );
    
    console.log( target.length ); // 2
    console.log( target.get( 1 ).label ); // 'bar'
    
    source.remove( 0 );
    console.log( target.length ); // 1
    console.log( target.get( 0 ).label ); // 'bar'
    
    Copy code

    or the factory driven by a custom callback:

    class FactoryClass {
    	public label: string;
    
    	constructor( data: { label: string } ) {
    		this.label = data.label;
    	}
    }
    
    const source = new Collection<{ label: string }>( { idProperty: 'label' } );
    const target = new Collection<FactoryClass>();
    
    target.bindTo( source ).as( FactoryClass );
    
    source.add( { label: 'foo' } );
    source.add( { label: 'bar' } );
    
    console.log( target.length ); // 2
    console.log( target.get( 1 ).label ); // 'bar'
    
    source.remove( 0 );
    console.log( target.length ); // 1
    console.log( target.get( 0 ).label ); // 'bar'
    
    Copy code

    or the factory out of property name:

    class FactoryClass {
    	public label: string;
    
    	constructor( data: { label: string } ) {
    		this.label = data.label;
    	}
    }
    
    const source = new Collection<{ label: string }>( { idProperty: 'label' } );
    const target = new Collection<FactoryClass>();
    
    target.bindTo( source ).as( FactoryClass );
    
    source.add( { label: 'foo' } );
    source.add( { label: 'bar' } );
    
    console.log( target.length ); // 2
    console.log( target.get( 1 ).label ); // 'bar'
    
    source.remove( 0 );
    console.log( target.length ); // 1
    console.log( target.get( 0 ).label ); // 'bar'
    
    Copy code

    It's possible to skip specified items by returning null value:

    class FactoryClass {
    	public label: string;
    
    	constructor( data: { label: string } ) {
    		this.label = data.label;
    	}
    }
    
    const source = new Collection<{ label: string }>( { idProperty: 'label' } );
    const target = new Collection<FactoryClass>();
    
    target.bindTo( source ).as( FactoryClass );
    
    source.add( { label: 'foo' } );
    source.add( { label: 'bar' } );
    
    console.log( target.length ); // 2
    console.log( target.get( 1 ).label ); // 'bar'
    
    source.remove( 0 );
    console.log( target.length ); // 1
    console.log( target.get( 0 ).label ); // 'bar'
    
    Copy code

    Note: clear can be used to break the binding.

    Type parameters

    S : extends Record<string, any>

    The type of externalCollection element.

    Parameters

    externalCollection : Collection<S>

    A collection to be bound.

    Returns

    CollectionBindToChain<S, T>

    The binding chain object.

  • Chevron-right icon

    clear() → void

    Removes all items from the collection and destroys the binding created using bindTo.

    Returns

    void

    Fires

  • Chevron-right icon

    delegate( events ) → EmitterMixinDelegateChain
    inherited

    Delegates selected events to another Emitter. For instance:

    emitterA.delegate( 'eventX' ).to( emitterB );
    emitterA.delegate( 'eventX', 'eventY' ).to( emitterC );
    
    Copy code

    then eventX is delegated (fired by) emitterB and emitterC along with data:

    emitterA.delegate( 'eventX' ).to( emitterB );
    emitterA.delegate( 'eventX', 'eventY' ).to( emitterC );
    
    Copy code

    and eventY is delegated (fired by) emitterC along with data:

    emitterA.delegate( 'eventX' ).to( emitterB );
    emitterA.delegate( 'eventX', 'eventY' ).to( emitterC );
    
    Copy code

    Parameters

    events : Array<string>

    Event names that will be delegated to another emitter.

    Returns

    EmitterMixinDelegateChain
  • Chevron-right icon

    filter( callback, [ ctx ] ) → Array<T>

    Returns an array with items for which the callback returned a true value.

    Parameters

    callback : ( item: T, index: number ) => boolean
    [ ctx ] : any

    Context in which the callback will be called.

    Returns

    Array<T>

    The array with matching items.

  • Chevron-right icon

    find( callback, [ ctx ] ) → undefined | T

    Finds the first item in the collection for which the callback returns a true value.

    Parameters

    callback : ( item: T, index: number ) => boolean
    [ ctx ] : any

    Context in which the callback will be called.

    Returns

    undefined | T

    The item for which callback returned a true value.

  • Chevron-right icon

    fire( eventOrInfo, args ) → GetEventInfo<TEvent>[ 'return' ]
    inherited

    Fires an event, executing all callbacks registered for it.

    The first parameter passed to callbacks is an EventInfo object, followed by the optional args provided in the fire() method call.

    Type parameters

    TEvent : extends BaseEvent

    The type describing the event. See BaseEvent.

    Parameters

    eventOrInfo : GetNameOrEventInfo<TEvent>

    The name of the event or EventInfo object if event is delegated.

    args : TEvent[ 'args' ]

    Additional arguments to be passed to the callbacks.

    Returns

    GetEventInfo<TEvent>[ 'return' ]

    By default the method returns undefined. However, the return value can be changed by listeners through modification of the evt.return's property (the event info is the first param of every callback).

  • Chevron-right icon

    forEach( callback, [ ctx ] ) → void

    Performs the specified action for each item in the collection.

    Parameters

    callback : ( item: T, index: number ) => unknown
    [ ctx ] : any

    Context in which the callback will be called.

    Returns

    void
  • Chevron-right icon

    get( idOrIndex ) → null | T

    Gets an item by its ID or index.

    Parameters

    idOrIndex : string | number

    The item ID or index in the collection.

    Returns

    null | T

    The requested item or null if such item does not exist.

  • Chevron-right icon

    getIndex( itemOrId ) → number

    Gets an index of an item in the collection. When an item is not defined in the collection, the index will equal -1.

    Parameters

    itemOrId : string | T

    The item or its ID in the collection.

    Returns

    number

    The index of a given item.

  • Chevron-right icon

    has( itemOrId ) → boolean

    Returns a Boolean indicating whether the collection contains an item.

    Parameters

    itemOrId : string | T

    The item or its ID in the collection.

    Returns

    boolean

    true if the collection contains the item, false otherwise.

  • Chevron-right icon

    listenTo( emitter, event, callback, [ options ] ) → void
    inherited

    Registers a callback function to be executed when an event is fired in a specific (emitter) object.

    Events can be grouped in namespaces using :. When namespaced event is fired, it additionally fires all callbacks for that namespace.

    // myEmitter.on( ... ) is a shorthand for myEmitter.listenTo( myEmitter, ... ).
    myEmitter.on( 'myGroup', genericCallback );
    myEmitter.on( 'myGroup:myEvent', specificCallback );
    
    // genericCallback is fired.
    myEmitter.fire( 'myGroup' );
    // both genericCallback and specificCallback are fired.
    myEmitter.fire( 'myGroup:myEvent' );
    // genericCallback is fired even though there are no callbacks for "foo".
    myEmitter.fire( 'myGroup:foo' );
    
    Copy code

    An event callback can stop the event and set the return value of the fire method.

    Type parameters

    TEvent : extends BaseEvent

    The type describing the event. See BaseEvent.

    Parameters

    emitter : Emitter

    The object that fires the event.

    event : TEvent[ 'name' ]

    The name of the event.

    callback : GetCallback<TEvent>

    The function to be called on event.

    [ options ] : GetCallbackOptions<TEvent>

    Additional options.

    Returns

    void
  • Chevron-right icon

    map( callback, [ ctx ] ) → Array<U>

    Executes the callback for each item in the collection and composes an array or values returned by this callback.

    Type parameters

    U

    The result type of the callback.

    Parameters

    callback : ( item: T, index: number ) => U
    [ ctx ] : any

    Context in which the callback will be called.

    Returns

    Array<U>

    The result of mapping.

  • Chevron-right icon

    off( event, callback ) → void
    inherited

    Stops executing the callback on the given event. Shorthand for this.stopListening( this, event, callback ).

    Parameters

    event : string

    The name of the event.

    callback : Function

    The function to stop being called.

    Returns

    void
  • Chevron-right icon

    on( event, callback, [ options ] ) → void
    inherited

    Registers a callback function to be executed when an event is fired.

    Shorthand for this.listenTo( this, event, callback, options ) (it makes the emitter listen on itself).

    Type parameters

    TEvent : extends BaseEvent

    The type descibing the event. See BaseEvent.

    Parameters

    event : TEvent[ 'name' ]

    The name of the event.

    callback : GetCallback<TEvent>

    The function to be called on event.

    [ options ] : GetCallbackOptions<TEvent>

    Additional options.

    Returns

    void
  • Chevron-right icon

    once( event, callback, [ options ] ) → void
    inherited

    Registers a callback function to be executed on the next time the event is fired only. This is similar to calling on followed by off in the callback.

    Type parameters

    TEvent : extends BaseEvent

    The type descibing the event. See BaseEvent.

    Parameters

    event : TEvent[ 'name' ]

    The name of the event.

    callback : GetCallback<TEvent>

    The function to be called on event.

    [ options ] : GetCallbackOptions<TEvent>

    Additional options.

    Returns

    void
  • Chevron-right icon

    remove( subject ) → T

    Removes an item from the collection.

    Parameters

    subject : string | number | T

    The item to remove, its ID or index in the collection.

    Returns

    T

    The removed item.

    Fires

  • Chevron-right icon

    stopDelegating( [ event ], [ emitter ] ) → void
    inherited

    Stops delegating events. It can be used at different levels:

    • To stop delegating all events.
    • To stop delegating a specific event to all emitters.
    • To stop delegating a specific event to a specific emitter.

    Parameters

    [ event ] : string

    The name of the event to stop delegating. If omitted, stops it all delegations.

    [ emitter ] : Emitter

    (requires event) The object to stop delegating a particular event to. If omitted, stops delegation of event to all emitters.

    Returns

    void
  • Chevron-right icon

    stopListening( [ emitter ], [ event ], [ callback ] ) → void
    inherited

    Stops listening for events. It can be used at different levels:

    • To stop listening to a specific callback.
    • To stop listening to a specific event.
    • To stop listening to all events fired by a specific object.
    • To stop listening to all events fired by all objects.

    Parameters

    [ emitter ] : Emitter

    The object to stop listening to. If omitted, stops it for all objects.

    [ event ] : string

    (Requires the emitter) The name of the event to stop listening to. If omitted, stops it for all events from emitter.

    [ callback ] : Function

    (Requires the event) The function to be removed from the call list for the given event.

    Returns

    void
  • Chevron-right icon

    _getItemIdBeforeAdding( item ) → string
    Lock icon private

    Returns an unique id property for a given item.

    The method will generate new id and assign it to the item if it doesn't have any.

    Parameters

    item : any

    Item to be added.

    Returns

    string
  • Chevron-right icon

    _remove( subject ) → tuple
    Lock icon private

    Core remove method implementation shared in other functions.

    In contrast this method does not fire the event-change event.

    Parameters

    subject : string | number | T

    The item to remove, its id or index in the collection.

    Returns

    tuple

    Returns an array with the removed item and its index.

    Fires

  • Chevron-right icon

    _setUpBindToBinding( factory ) → void
    Lock icon private

    Finalizes and activates a binding initiated by bindTo.

    Type parameters

    S

    Parameters

    factory : ( item: S ) => ( null | T )

    A function which produces collection items.

    Returns

    void

Events

  • Chevron-right icon

    add( eventInfo, item, index )

    Fired when an item is added to the collection.

    Parameters

    eventInfo : EventInfo

    An object containing information about the fired event.

    item : T

    The added item.

    index : number

    An index where the addition occurred.

  • Chevron-right icon

    change( eventInfo, data )

    Fired when the collection was changed due to adding or removing items.

    Parameters

    eventInfo : EventInfo

    An object containing information about the fired event.

    data : CollectionChangeEventData<T>

    Changed items.

  • Chevron-right icon

    remove( eventInfo, item, index )

    Fired when an item is removed from the collection.

    Parameters

    eventInfo : EventInfo

    An object containing information about the fired event.

    item : T

    The removed item.

    index : number

    Index from which item was removed.