Home Reference Source
import {PrioritisedArray} from 'arva-js/data/PrioritisedArray.js'
public class | source

PrioritisedArray

Extends:

Array → PrioritisedArray

Direct Subclass:

LocalPrioritisedArray

An array of two-way bound data Models that are automatically synced with the currently used DataSource

Constructor Summary

Public Constructor
public

constructor(dataType: Function, dataSource: DataSource, dataSnapshot: Snapshot, options: Object, modelOptions: Object): PrioritisedArray

Member Summary

Public Members
public get

The number of items in the (synchronized or local) data set.

public set

length(value: Number): *: *

A setter on the length is necessary because internal methods of Array modify the lngth.

Method Summary

Public Methods
public

add(model: Model | Object, prevSiblingId: String): Object

Adds a model instance to the rear of the PrioritisedArray, and emits a 'child_added' and possibly 'new_child' event after successful addition.

public

findById(id: *): Model

Finds an item based on its Id in the datasource.

public

insertAt(model: Model, position: Number): Object

Inserts a model instance at the given position of the PrioritisedArray, and recalculates the priority (position) of all models after the inserted position.

public

off(event: String, handler: Function, context: Object): void

Removes subscription to events emitted by this PrioritisedArray.

public

on(event: String, handler: Function, context: Object): void

Subscribes to events emitted by this PrioritisedArray.

public

once(event: String, handler: Function, context: Object): Promise

Subscribes to the given event type exactly once; it automatically unsubscribes after the first time it is triggered.

public

push(model: Object | Model): Model

Adds a model or object to the end of the list.

public

remove(position: Number): void

Removes the model instance at the given position.

Public Constructors

public constructor(dataType: Function, dataSource: DataSource, dataSnapshot: Snapshot, options: Object, modelOptions: Object): PrioritisedArray source

Params:

NameTypeAttributeDescription
dataType Function

DataType of the models being added to the PrioritisedArray.

dataSource DataSource
  • optional

dataSource to load the models from. If none is given, a new DataSource is made with a path guessed from the model's DataType name.

dataSnapshot Snapshot
  • optional

snapshot already containing model data. Prevents initial subscription on all values in the DataSource.

options Object
  • optional

options to pass to the dataSource if none is provided and a new one is constructed.

modelOptions Object
  • optional

options to merge into the construction of every new Model.

Return:

PrioritisedArray

PrioritisedArray instance.

Public Members

public get length: Number: * source

The number of items in the (synchronized or local) data set.

Return:

Number

public set length(value: Number): *: * source

A setter on the length is necessary because internal methods of Array modify the lngth. It won't change the length thoough

Return:

*

Public Methods

public add(model: Model | Object, prevSiblingId: String): Object source

Adds a model instance to the rear of the PrioritisedArray, and emits a 'child_added' and possibly 'new_child' event after successful addition.

Params:

NameTypeAttributeDescription
model Model | Object

Instance of a Model.

prevSiblingId String

ID of the model preceding the one that will be added.

Return:

Object

Same model as the one originally passed as parameter.

public findById(id: *): Model source

Finds an item based on its Id in the datasource.

Params:

NameTypeAttributeDescription
id *

Return:

Model

public insertAt(model: Model, position: Number): Object source

Inserts a model instance at the given position of the PrioritisedArray, and recalculates the priority (position) of all models after the inserted position.

Params:

NameTypeAttributeDescription
model Model

Subclass of Model

position Number

Zero-based index where to put the new model instance.

Return:

Object

Same model as the one originally passed as parameter.

public off(event: String, handler: Function, context: Object): void source

Removes subscription to events emitted by this PrioritisedArray. If no handler or context is given, all handlers for the given event are removed. If no parameters are given at all, all event types will have their handlers removed.

Params:

NameTypeAttributeDescription
event String

One of the following Event Types: 'value', 'child_changed', 'child_moved', 'child_removed'.

handler Function

Function to remove from event callbacks.

context Object

Object to bind the given callback function to.

Return:

void

public on(event: String, handler: Function, context: Object): void source

Subscribes to events emitted by this PrioritisedArray.

Params:

NameTypeAttributeDescription
event String

One of the following Event Types: 'value', 'child_changed', 'child_moved', 'child_removed'.

handler Function

Function that is called when the given event type is emitted.

context Object

Optional: context of 'this' inside the handler function when it is called.

Return:

void

public once(event: String, handler: Function, context: Object): Promise source

Subscribes to the given event type exactly once; it automatically unsubscribes after the first time it is triggered.

Params:

NameTypeAttributeDescription
event String

One of the following Event Types: 'value', 'child_changed', 'child_moved', 'child_removed'.

handler Function
  • optional

Function that is called when the given event type is emitted.

context Object
  • optional

context of 'this' inside the handler function when it is called.

Return:

Promise

If no callback function provided, a promise that resolves once the event has happened

public push(model: Object | Model): Model source

Adds a model or object to the end of the list.

Params:

NameTypeAttributeDescription
model Object | Model

Return:

Model

The newly inserted model

public remove(position: Number): void source

Removes the model instance at the given position. Does not remove the model from the datasource, to do that call model.remove() directly, or PrioArray[index].remove().

Params:

NameTypeAttributeDescription
position Number

Index in the PrioritisedArray of the model to remove.

Return:

void