Collection
A class which encapsulates a keyed,
filterable, sortable
collection of objects. Entries may not be atomic data types such as string or number.
The entries are keyed by their id which is determined by interrogating the idProperty.
To filter a Collection, add a CollectionFilter using the addFilter method. A Filter config object may be specified here which will be promoted to a CollectionFilter instance.
To sort a Collection, add a CollectionSorter using the addSorter method. A Sorter config object may be specified here which will be promoted to a CollectionSorter instance.
Configs
4
Configs
4Automatically apply filters on item add.
Specify the names or index configs of properties which are to be indexed for fast lookup.
Index configs use the format { property : string, unique : boolean }. Unique indices stores one index
per entry, non-unique stores a Set. If left out, unique defaults to true
| Parameter | Type | Description |
|---|---|---|
property | String | Property to index by |
unique | Boolean |
|
Specify the name of the property of added objects which provides the lookup key
A Sorter, or Sorter config object, or an array of these, to use to sort this Collection.
Properties
12
Properties
12The set of all values of this Collection regardless of filters applied.
The number of items in this collection. Note that this honours filtering. See totalCount;
A filter function which encapsulates the Filters for this Collection.
| Parameter | Type | Description |
|---|---|---|
value | * | Value to compare |
Returns true for value valid for including
The Collection of Filters for this Collection.
A counter which is incremented whenever the Collection is mutated in a meaningful way.
If a splice call results in no net replacement, removal or addition,
then the generation will not be incremented.
The property name used to extract item ids from added objects.
A flag which is true if this Collection has active filters.
The Collection of Sorters for this Collection.
A sorter function which encapsulates the Sorters for this Collection.
| Parameter | Type | Description |
|---|---|---|
first | * | First value to compare |
second | * | Second value to compare |
Returns 1 if first value is a greater unit than second value, -1 if the opposite is true or 0 if they're equal
The number of items in this collection regardless of filtering.
The set of values of this Collection. If this Collection isFiltered, this yields the filtered data set.
Setting this property replaces the data set.
Functions
20
Functions
20Adds items to this Collection. Multiple new items may be passed.
By default, new items are appended to the existing values.
Any sorters sorters present are re-run.
Any filters filters present are re-run.
Note that if application functionality requires add and remove, the splice operation is preferred as it performs both operations in an atomic manner
| Parameter | Type | Description |
|---|---|---|
items | Object | The item(s) to add. |
Adds a Filter to the Collection of Filters which are operating on this Collection.
A Filter may be an specified as an instantiated CollectionFilter, or a config object for a CollectionFilter of the form
{
property : 'age',
operator : '>=',
value : 21
}
Note that by default, a Filter replaces a Filter with the same property to make
it easy to change existing Filters. A Filter's id is its property by default. You
can avoid this and add multiple Filters for one property by configuring Filters with ids.
A Filter may also be specified as a function which filters candidate objects eg:
candidate => candidate.customerDetails.age >= 21
| Parameter | Type | Description |
|---|---|---|
filter | CollectionFilterConfig | CollectionFilter | A Filter or Filter configuration object to add to the Collection of Filters operating on this Collection. |
The resulting Filter to make it easy to remove Filters.
Adds a Sorter to the Collection of Sorters which are operating on this Collection.
A Sorter may be specified as an instantiated CollectionSorter, or a config object for a CollectionSorter of the form
{
property : 'age',
direction : 'desc'
}
Note that by default, a Sorter replaces a Sorter with the same property to make
it easy to change existing Sorters. A Sorter's id is its property by default. You
can avoid this and add multiple Sorters for one property by configuring Sorters with ids.
A Sorter may also be specified as a function which compares two objects eg:
(lhs, rhs) => lhs.customerDetails.age - rhs.customerDetails.age
| Parameter | Type | Description |
|---|---|---|
sorter | CollectionSorterConfig | A Sorter configuration object to add to the Collection of Sorters operating on this Collection. |
The resulting Sorter to make it easy to remove Sorters.
Change the id of an existing member by mutating its idProperty.
| Parameter | Type | Description |
|---|---|---|
item | String | Number | Object | The item or id of the item to change. |
newId | String | Number | The id to set in the existing member. |
Clears this collection.
Compares the content of this Collection with the content of the passed Collection or
with the passed array. Order insensitive. This returns true if the two objects passed
contain the same set of items.
| Parameter | Type | Description |
|---|---|---|
other | Collection | Array | The Collection or array to compare with. |
map | function | Optionally a function to convert the items into a comparable object
to compare. For example |
true if the two objects passed have the same content.
Returns the first item in this Collection which elicits a truthy return value from the passed function.
| Parameter | Type | Description |
|---|---|---|
fn | function | A function, which, when passed an item, returns |
ignoreFilters | Boolean | Pass |
The matched item, or undefined.
Return the index of the item with the specified key having the specified value.
By default, filtering is taken into account and this returns the index in the filtered dataset if present. To
bypass this, pass the third parameter as true.
Only useful for indices configured with unique: true.
| Parameter | Type | Description |
|---|---|---|
propertyName | String | The name of the property to test. |
value | * | The value to test for. |
ignoreFilters | Boolean | Pass |
The index of the item or -1 if not found for unique indices
Return the item with the specified key having the specified value.
By default, filtering is taken into account. To bypass this, pass the third parameter as true.
For indices configured with unique: false, a Set of items will be returned.
| Parameter | Type | Description |
|---|---|---|
propertyName | String | The name of the property to test. |
value | * | The value to test for. |
ignoreFilters | Boolean | Pass |
The found item or Set of items or null
Executes the passed function for each item in this Collection, passing in the item, ths index, and the full item array.
| Parameter | Type | Description |
|---|---|---|
fn | function | The function to execute. |
ignoreFilters | Boolean | Pass |
Returns the item with the passed id. By default, filtered are honoured, and
if the item with the requested id is filtered out, nothing will be returned.
To return the item even if it has been filtered out, pass the second parameter as true.
| Parameter | Type | Description |
|---|---|---|
id | * | The |
ignoreFilters | Boolean | Pass |
The found item, or undefined.
Returns the item with passed property name equal to the passed value. By default,
filtered are honoured, and if the item with the requested id is filtered out,
nothing will be returned.
To return the item even if it has been filtered out, pass the third parameter as true.
| Parameter | Type | Description |
|---|---|---|
propertyName | String | The property to test. |
value | * | The value to find. |
ignoreFilters | Boolean | Pass |
The found item, or undefined.
Returns true if this Collection includes an item with the same id as the passed item.
By default, filtering is honoured, so if the item in question has been added,
but is currently filtered out of visibility, false will be returned.
To query inclusion in the master, unfiltered dataset, pass the second parameter as true;
| Parameter | Type | Description |
|---|---|---|
item | Object | String | Number | The item to find, or an |
ignoreFilters | Boolean | Pass |
True if the passed item is found.
Returns the index of the item with the same id as the passed item.
By default, filtering is honoured, so if the item in question has been added, but is currently filtered out of
visibility, -1 will be returned.
To find the index in the master, unfiltered dataset, pass the second parameter as true;
| Parameter | Type | Description |
|---|---|---|
item | Object | String | Number | The item to find, or an |
ignoreFilters | Boolean | Pass |
The index of the item, or -1 if not found.
Extracts ths content of this Collection into an array based upon the passed value extraction function.
| Parameter | Type | Description |
|---|---|---|
fn | function | A function, which, when passed an item, returns a value to place into the resulting array. |
ignoreFilters | Boolean | Pass |
An array of values extracted from this Collection.
This method ensures that every item in this Collection is replaced by the matched by
id item in the other Collection.
By default, any items in this Collection which are not in the other Collection are removed.
If the second parameter is passed as false, then items which are not in the other
Collection are not removed.
This can be used for example when updating a selected record Collection when a new
Store or new store dataset arrives. The selected Collection must reference the latest
versions of the selected record ids
| Parameter | Type | Description |
|---|---|---|
other | Collection | The Collection whose items to match. |
Moves an individual item, or a block of items to another location.
| Parameter | Type | Description |
|---|---|---|
items | Object | Object[] | The item/items to move. |
beforeItem | Object | the item to insert the first item before. If omitted, the |
The new index of the item.
Returns an accumulated value based on the passed function similar to Array.reduce.
| Parameter | Type | Description |
|---|---|---|
fn | function | A function, which, when passed an accumulator value and an item, returns the new accumulator value. |
ignoreFilters | Boolean | Pass |
accumulator | * | The initial value of the accumulator. |
the final accumulator value.
Removes items from this Collection. Multiple items may be passed.
Any sorters sorters present are re-run.
Any filters filters present are re-run.
Note that if application functionality requires add and remove, the splice operation is preferred as it performs both operations in an atomic manner
| Parameter | Type | Description |
|---|---|---|
items | Object | The item(s) to remove. |
The core data set mutation method. Removes and adds at the same time. Analogous
to the Array splice method.
Note that if items that are specified for removal are also in the toAdd array,
then those items are not removed then appended. They remain in the same position
relative to all remaining items.
| Parameter | Type | Description |
|---|---|---|
index | Number | Index at which to remove a block of items. Only valid if the
second, |
toRemove | Object[] | Number | Either the number of items to remove starting
at the passed |
toAdd | Object[] | Object | An item, or an array of items to add. |
Events
2
Events
2Fired when items are added, replace or removed
// Adding a listener using the "on" method
collection.on('change', ({ action, source, removed, added, replaced, oldCount }) => {
});| Parameter | Type | Description |
|---|---|---|
action | splice | clear | replaceValues | move | sort | filter | The underlying operation
which caused data change. May be |
source | Collection | This Collection. |
removed | Object[] | An array of removed items. If the |
added | Object[] | An array of added items. If the |
replaced | Object[] | An array of replacements, each entry of which contains |
oldCount | Number | The number of items in the full, unfiltered collection prior to the splice operation. |
Fired when a splice operation is requested but the operation is a no-op and has caused no change to this Collection's dataset. The splice method's parameters are passed for reference.
// Adding a listener using the "on" method
collection.on('noChange', ({ index, toRemove, toAdd }) => {
});| Parameter | Type | Description |
|---|---|---|
index | Number | Index at which to remove a block of items. |
toRemove | Object[] | Number | Either the number of items to remove starting
at the passed |
toAdd | Object[] | Object | An item, or an array of items to add. |
Event handlers
2
Event handlers
2Called when items are added, replace or removed
new Collection({
onChange({ action, source, removed, added, replaced, oldCount }) {
}
});| Parameter | Type | Description |
|---|---|---|
action | splice | clear | replaceValues | move | sort | filter | The underlying operation
which caused data change. May be |
source | Collection | This Collection. |
removed | Object[] | An array of removed items. If the |
added | Object[] | An array of added items. If the |
replaced | Object[] | An array of replacements, each entry of which contains |
oldCount | Number | The number of items in the full, unfiltered collection prior to the splice operation. |
Called when a splice operation is requested but the operation is a no-op and has caused no change to this Collection's dataset. The splice method's parameters are passed for reference.
new Collection({
onNoChange({ index, toRemove, toAdd }) {
}
});| Parameter | Type | Description |
|---|---|---|
index | Number | Index at which to remove a block of items. |
toRemove | Object[] | Number | Either the number of items to remove starting
at the passed |
toAdd | Object[] | Object | An item, or an array of items to add. |