Class: ModelCollection

Rekord. ModelCollection

new Rekord.ModelCollection(database, models, remoteData)

An extension of the Rekord.Collection class for Rekord.Model instances.

Name Type Default Description
database Rekord.Database

The database for the models in this collection.

models Array.<modelInput> optional

The initial array of models in this collection.

remoteData Boolean false optional

If the models array is from a remote source. Remote sources place the model directly into the database while local sources aren't stored in the database until they're saved.

See:
  • Rekord.Models.boot
  • Rekord.Models.collect

Extends

Members

comparatorcomparisonCallback

A comparator to keep the collection sorted with.

databaseRekord.Database

The database for the models in this collection.

The map of models which keeps an index (by model key) of the models.

Methods

add(input, delaySort, remoteData){Rekord.ModelCollection}

Adds a model to this collection - sorting the collection if a comparator is set on this collection and delaySort is not a specified or a true value.

Name Type Default Description
input modelInput

The model to add to this collection.

delaySort Boolean false optional

Whether automatic sorting should be delayed until the user manually calls sort.

remoteData Boolean false optional

If the model is from a remote source. Remote sources place the model directly into the database while local sources aren't stored in the database until they're saved.

Fires:
Returns:
Type Description
Rekord.ModelCollection - The reference to this collection.

addAll(models, delaySort, remoteData){Rekord.ModelCollection}

Adds all models in the given array to this collection - sorting the collection if a comparator is set on this collection and delaySort is not specified or a true value.

Name Type Default Description
models Array.<modelInput>

The models to add to this collection.

delaySort Boolean false optional

Whether automatic sorting should be delayed until the user manually calls sort.

remoteData Boolean false optional

If the model is from a remote source. Remote sources place the model directly into the database while local sources aren't stored in the database until they're saved.

Fires:
Returns:
Type Description
Rekord.ModelCollection - The reference to this collection.

inherited addComparator(comparator, nullsFirst){Rekord.Collection}

Adds a comparator to the existing comparator. This added comparator is ran after the current comparator when it finds two elements equal. If no comparator exists on this collection then it's set to the given comparator.

Name Type Default Description
comparator ComparatorInput

The comparator input to convert to a comparison function.

nullsFirst Boolean false optional

When a comparison is done involving a null/undefined value this can determine which is ordered before the other.

See:
Fires:
Returns:
Type Description
Rekord.Collection

inherited aggregate(resolver, validator, process, getResult){Any}

Iterates over all elements in this collection and passes them through the resolver function. The returned value is passed through the validator function and if that returns true the resolved value is passed through the process function. After iteration, the getResult function is executed and the returned value is returned by this function.

Name Type Description
resolver function

The function which takes an element in this collection and returns a value based on that element.

validator function

The function which takes the resolved value and determines whether it passes some test.

process function

The function which is given the resolved value if it passes the test.

getResult function

The function which is executed at the end of iteration and the result is is returned by this function.

Returns:
Type Description
Any - The value returned by getResult.

Averages all numbers resolved from the given property expression and returns the result.

var c = Rekord.collect([2, 3, 4]);
c.avg(); // 3
var d = Rekord.collect([{age: 5}, {age: 4}, {age: 2}]);
d.avg('age'); // 3.66666
Name Type Description
numbers propertyResolverInput optional

The expression which converts an element in this collection to a number.

See:
  • Rekord.createNumberResolver
Returns:
Type Description
Number - The average of all valid numbers found in this collection.

buildKeyFromInput(input){modelKey}

Takes input provided to the collection for adding, removing, or querying and generates the key which uniquely identifies a model.

Name Type Description
input modelInput

The input to convert to a key.

Returns:
Type Description
modelKey - The key built from the input.

cancelWhere(reset, properties, value, equals){Rekord.ModelCollection}

Calls Rekord.Model#$cancel on models in this collection that meet the given where expression.

Name Type Default Description
reset Boolean false optional

If reset is true and the model doesn't have a saved state - Rekord.Model#$reset will be called.

properties whereInput optional

See Rekord.createWhere

value Any optional

See Rekord.createWhere

equals equalityCallback Rekord.equalsStrict optional

See Rekord.createWhere

See:
Returns:
Type Description
Rekord.ModelCollection - The reference to this collection.

inherited change(callback, context){function}

Adds a listener for change events on this collection.

Name Type Description
callback function

A function to call every time a change occurs in this collection.

context Object optional

The desired context (this) for the given callback function.

See:
Returns:
Type Description
function - A function to call to stop listening for change events.

inherited chunk(chunkSize, out){Array}

Breaks up the collection into an array of arrays of a maximum size (chunks). A destination array can be used to avoid re-allocating arrays.

var c = Rekord.collect([1, 2, 3, 4, 5, 6, 7, 8, 9]);
c.chunk(4); // [[1, 2, 3, 4], [5, 6, 7, 8], [9]]
Name Type Description
chunkSize Number

The maximum number of elements that can exist in a chunk.

out Array optional

The destination array to place the chunks.

Returns:
Type Description
Array - The array of chunks of elements taken from this collection.

Clears all elements from this collection.

var a = Rekord.collect(1, 2, 3, 4);
a.clear(); // []
Fires:
Returns:
Type Description
Rekord.Collection - The reference to this collection.

clone(cloneModels, cloneProperties){Rekord.ModelCollection}

Returns a clone of this collection. Optionally the models in this collection can also be cloned.

Name Type Default Description
cloneModels Boolean false optional

Whether or not the models should be cloned as well.

cloneProperties Boolean optional

The properties object which defines what fields should be given a different (non-cloned) value and which relations need to be cloned.

See:
  • Rekord.Model#$clone
Returns:
Type Description
Rekord.ModelCollection - The reference to a clone collection.

Returns an empty clone of this collection.

Returns:
Type Description
Rekord.ModelCollection - The reference to a clone collection.

inherited complement(collection, out, equals){Array}

Returns a collection of elements that exist in the given collection but not in this collection.

var a = Rekord.collect(1, 2, 3, 4);
var b = Rekord.collect(1, 3, 5);
var c = a.complement( b ); // [5]
Name Type Default Description
collection Array

The array of elements that could exist in the resulting collection.

out Array this.cloneEmpty() optional

The array to place the elements that exist in given collection but not in this collection. If this is not given - a collection of this type will be created.

equals equalityCallback Rekord.equalsStrict optional

The function which determines whether one of the elements that exist in this collection are equivalent to an element that exists in the given collection.

Returns:
Type Description
Array - The collection of elements that exist in the given collection and not this collection.

inherited contains(properties, value, equals){Boolean}

Determines whether at least one element in this collection matches the given criteria.

var c = Rekord.collect([{age: 2}, {age: 6}]);
c.contains('age', 2); // true
c.contains('age', 3); // false
c.contains('age'); // true
c.contains('name'); // false
Name Type Default Description
properties whereInput optional

The expression used to create a function to test the elements in this collection.

value Any optional

When the first argument is a string this argument will be treated as a value to compare to the value of the named property on the object passed through the filter function.

equals equalityCallback Rekord.equalsStrict optional

An alternative function can be used to compare to values.

See:
Returns:
Type Description
Boolean - True if any of the elements passed the test function, otherwise false.

inherited count(properties){Number}

Counts the number of elements in this collection that has a value for the given property expression.

var c = Rekord.collect([{age: 2}, {age: 3}, {taco: 4}]);
c.count('age'); // 2
c.count('taco'); // 1
c.count(); // 3
Name Type Description
properties propertyResolverInput optional

The expression which converts one value into another.

See:
Returns:
Type Description
Number - The number of elements that had values for the property expression.

inherited countWhere(properties, value, equals){Number}

Counts the number of elements in this collection that past the test function generated by Rekord.createWhere.

var c = Rekord.collect([{name: 't1', done: 1}, {name: 't2', done: 0}, {name: 't3', done: 1}, {name: 't4'}]);
c.countWhere('done'); // 3
c.countWhere('done', 0); // 1
c.countWhere('done', 1); // 2
Name Type Default Description
properties whereInput optional

The expression used to create a function to test the elements in this collection.

value Any optional

When the first argument is a string this argument will be treated as a value to compare to the value of the named property on the object passed through the filter function.

equals equalityCallback Rekord.equalsStrict optional

An alternative function can be used to compare to values.

See:
Returns:
Type Description
Number - The number of elements in the collection that passed the test.

discardWhere(properties, value, equals){Rekord.ModelCollection}

Calls Rekord.Model#$discard on models in this collection that meet the given where expression.

Name Type Default Description
properties whereInput optional

See Rekord.createWhere

value Any optional

See Rekord.createWhere

equals equalityCallback Rekord.equalsStrict optional

See Rekord.createWhere

See:
Returns:
Type Description
Rekord.ModelCollection - The reference to this collection.

Iterates over each element in this collection and passes the element and it's index to the given function. An optional function context can be given.

Name Type Description
callback function

The function to invoke for each element of this collection passing the element and the index where it exists.

context Object optional

The context to the callback function.

Returns:
Type Description
Rekord.Collection - The reference to this collection.

inherited eachWhere(callback, properties, value, equals){Rekord.Collection}

Iterates over each element in this collection that matches the where expression and passes the element and it's index to the given function.

Name Type Default Description
callback function

The function to invoke for each element of this collection passing the element and the index where it exists.

properties whereInput optional

See Rekord.createWhere

value Any optional

See Rekord.createWhere

equals equalityCallback Rekord.equalsStrict optional

See Rekord.createWhere

See:
Returns:
Type Description
Rekord.Collection - The reference to this collection.

filtered(whereProperties, whereValue, whereEquals){Rekord.FilteredModelCollection}

Creates a sub view of this collection known as a filtered collection. The resulting collection changes when this collection changes. Any time an element is added or removed to this collection it may be added or removed from the filtered collection if it fits the filter function. The filter function is created by passing the arguments of this function to Rekord.createWhere.

Name Type Description
whereProperties whereInput optional

See Rekord.createWhere

whereValue Any optional

See Rekord.createWhere

whereEquals equalityCallback optional

See Rekord.createWhere

See:
Returns:
Type Description
Rekord.FilteredModelCollection - The newly created live filtered view of this collection.

inherited first(properties, delim){Any}

Returns the first non-null value in this collection given a property expression. If no non-null values exist for the given property expression, then undefined will be returned.

var c = Rekord.collect([{x: 5}, {y: 6}, {y: 4}, {z: 7}]);
c.first('y'); // 6
c.first(); // {x: 5}
Name Type Default Description
properties propertyResolverInput optional

The expression which converts one value into another.

delim String ',' optional

A delimiter to use to join multiple properties into a string.

See:
Returns:
Type Description
Any -

inherited firstWhere(whereProperties, whereValue, whereEquals){Any}

Returns the first element where the given expression is true.

var c = Rekord.collect([{x: 5}, {y: 6}, {y: 6, age: 8}, {z: 7}]);
c.firstWhere('y', 6); // {x: 6}
c.firstWhere(); // {x: 5}
Name Type Default Description
whereProperties whereInput optional

The expression used to create a function to test the elements in this collection.

whereValue Any optional

When the first argument is a string this argument will be treated as a value to compare to the value of the named property on the object passed through the filter function.

whereEquals equalityCallback Rekord.equalsStrict optional

An alternative function can be used to compare to values.

See:
Returns:
Type Description
Any - The first element in this collection that matches the given expression.

Returns the model in this collection with the given key.

Name Type Description
key modelKey

The key of the model to return.

Returns:
Type Description
Rekord.Model - The model instance for the given key, or undefined if a model wasn't found.

Groups the elements into sub collections given some property expression to use as the value to group by.

var c = Rekord.collect([
 { name: 'Tom', age: 6, group: 'X' },
 { name: 'Jon', age: 7, group: 'X' },
 { name: 'Rob', age: 8, group: 'X' },
 { name: 'Bon', age: 9, group: 'Y' },
 { name: 'Ran', age: 10, group: 'Y' },
 { name: 'Man', age: 11, group: 'Y' },
 { name: 'Tac', age: 12, group: 'Z' }
]);

c.group({by: 'group'});
// [{group: 'X', $count: 3, $group: [...]},
//  {group: 'Y', $count: 3, $group: [...]},
//  {group: 'Z', $count: 1, $group: [.]}]

c.group({by: 'group', select: {age: 'avg', name: 'first'}});
// [{group: 'X', age: 7, name: 'Tom', $count: 3, $group: [...]},
//  {group: 'Y', age: 9, name: 'Bon', $count: 3, $group: [...]},
//  {group: 'Z', age: 12, name: 'Tac', $count: 1, $group: [.]}]

c.group({by: 'group', track: false, count: false});
// [{group: 'X'}, {group: 'Y'}, {group: 'Z'}]

var havingMoreThanOne = function(grouping, groupElements) {
 return groupElements.length > 0;
};
c.group({by: 'group', select: {age: 'avg'}, comparator: '-age', having: havingMoreThanOne, track: false, count: false});
// [{group: 'Y', age: 9},
//  {group: 'X', age: 7}]
Name Type Description
grouping Object

An object specifying how elements in this collection are to be grouped and what properties from the elements should be aggregated in the resulting groupings.

 - `by`: A property expression that resolves how elements will be grouped.
 - `bySeparator`: When an array or object property expression is specified, this is the string that joins them.
 - `select`: An object which contains properties that should be aggregated where the value is the aggregate collection function to call (sum, avg, count, first, last, etc).
 - `having`: A having expression which takes a grouping and the grouped elements and determines whether the grouping should be in the final result.
 - `comparator`: A comparator for sorting the resulting collection of groupings.
 - `comparatorNullsFirst`: Whether nulls should be sorted to the top.
 - `track`: Whether all elements in the group should exist in a collection in the `$group` property of each grouping.
 - `count`: Whether the number of elements in the group should be placed in the `$count` property of each grouping.
Returns:
Type Description
Rekord.Collection - A collection of groupings.

Returns whether this collection contains a model with the given key.

Name Type Description
key modelKey

The key of the model to check for existence.

Returns:
Type Description
Boolean - True if a model with the given key exists in this collection, otherwise false.

indexOf(input){Number}

Returns the index of the given model in this collection or returns -1 if the model doesn't exist in this collection.

Name Type Description
input modelInput

The model to search for.

Returns:
Type Description
Number - The index of the model in this collection or -1 if it was not found.

init(database, models, remoteData){Rekord.ModelCollection}

Initializes the model collection by setting the database, the initial set of models, and whether the initial set of models is from a remote source.

Name Type Default Description
database Rekord.Database

The database for the models in this collection.

models Array.<modelInput> optional

The initial array of models in this collection.

remoteData Boolean false optional

If the models array is from a remote source. Remote sources place the model directly into the database while local sources aren't stored in the database until they're saved.

Fires:
Returns:
Type Description
Rekord.ModelCollection - The reference to this collection.
See:
Fires:
Returns:
Type Description
Rekord.ModelCollection - The reference to this collection.

inherited intersect(collection, out, equals){Array}

Returns a collection of elements that are shared between this collection and the given collection.

var a = Rekord.collect(1, 2, 3, 4);
var b = Rekord.collect(1, 3, 5);
var c = a.intersect( b ); // [1, 3]
Name Type Default Description
collection Array

The collection of elements to intersect with this collection.

out Array this.cloneEmpty() optional

The array to place the elements that exist in both this collection and the given collection. If this is not given - a collection of this type will be created.

equals equalityCallback Rekord.equalsStrict optional

The function which determines whether one of the elements that exist in this collection are equivalent to an element that exists in the given collection.

Returns:
Type Description
Array - The collection of elements that exist in both collections.

inherited isSorted(comparator, nullsFirst){Boolean}

Determines if the collection is currently sorted based on the current comparator of the collection unless a comparator is given

Name Type Default Description
comparator ComparatorInput optional

The comparator input to convert to a comparison function.

nullsFirst Boolean false optional

When a comparison is done involving a null/undefined value this can determine which is ordered before the other.

See:
Returns:
Type Description
Boolean

Returns the array of keys that correspond to the models in this collection.

Returns:
Type Description
Array.<modelKey> - The array of model keys.

inherited last(properties, delim){Any}

Returns the last non-null value in this collection given a property expression. If no non-null values exist for the given property expression, then undefined will be returned.

var c = Rekord.collect([{x: 5}, {y: 6}, {y: 4}, {z: 7}]);
c.last('y'); // 4
c.last(); // {z: 7}
Name Type Default Description
properties propertyResolverInput optional

The expression which converts one value into another.

delim String ',' optional

A delimiter to use to join multiple properties into a string.

See:
Returns:
Type Description
Any -

inherited lastWhere(properties, value, equals){Any}

Returns the last element where the given expression is true.

var c = Rekord.collect([{x: 5}, {y: 6}, {y: 6, age: 8}, {z: 7}]);
c.lastWhere('y', 6); // {x: 6, age: 8}
c.lastWhere(); // {z: 7}
Name Type Default Description
properties whereInput optional

The expression used to create a function to test the elements in this collection.

value Any optional

When the first argument is a string this argument will be treated as a value to compare to the value of the named property on the object passed through the filter function.

equals equalityCallback Rekord.equalsStrict optional

An alternative function can be used to compare to values.

See:
Returns:
Type Description
Any - The last element in this collection that matches the given expression.

inherited max(properties, delim, startingValue){Any}

Returns the maximum value for the given property expression out of all the elements this collection.

var c = Rekord.collect({age: 6}, {age: 5}, {notage: 5});
c.max('age');  // 6
Name Type Default Description
properties propertyResolverInput optional

The expression which takes an element in this container and resolves a value that can be compared to the current maximum.

delim String ',' optional

A delimiter to use to join multiple properties into a string.

startingValue Any optional

The initial maximum value. If a value is specified, it's compared against all elements in this collection until the comparator function finds a more maximal value. If it doesn't - this is the value returned.

See:
Returns:
Type Description
Any - The maximum value found.

inherited maxModel(comparator, startingValue){Any}

Returns the element with the maximum value given a comparator.

var c = Rekord.collect({age: 4}, {age: 5}, {age: 6}, {age: 3});
c.maxModel('age'); // {age: 6}
c.maxModel('-age'); // {age: 3}
Name Type Description
comparator comparatorInput

The comparator which calculates the maximum model.

startingValue Any optional

The initial maximum value. If a value is specified, it's compared against all elements in this collection until the comparator function finds a more maximal value. If it doesn't - this is the value returned.

See:
Returns:
Type Description
Any - The maximum element in the collection given the comparator function.

inherited min(properties, delim, startingValue){Any}

Returns the minimum value for the given property expression out of all the elements this collection.

var c = Rekord.collect({age: 6}, {age: 5}, {notage: 5});
c.min('age');  // 5
Name Type Default Description
properties propertyResolverInput optional

The expression which takes an element in this container and resolves a value that can be compared to the current minimum.

delim String ',' optional

A delimiter to use to join multiple properties into a string.

startingValue Any optional

The initial minimum value. If a value is specified, it's compared against all elements in this collection until the comparator function finds a more minimal value. If it doesn't - this is the value returned.

See:
Returns:
Type Description
Any - The minimum value found.

inherited minModel(comparator, startingValue){Any}

Returns the element with the minimum value given a comparator.

var c = Rekord.collect({age: 4}, {age: 5}, {age: 6}, {age: 3});
c.minModel('age'); // {age: 3}
c.minModel('-age'); // {age: 6}
Name Type Description
comparator comparatorInput

The comparator which calculates the minimum model.

startingValue Any optional

The initial minimum value. If a value is specified, it's compared against all elements in this collection until the comparator function finds a more minimal value. If it doesn't - this is the value returned.

See:
Returns:
Type Description
Any - The minimum element in the collection given the comparator function.

inherited on(events, callback, context){function}

Listens for every occurrence of the given events and invokes the callback each time any of them are triggered.

Name Type Description
events String | Array

The event or events to listen to.

callback function

The function to invoke when any of the events are invoked.

context Object optional

The value of this when the callback is invoked. If not specified, the reference of the object this function exists on will be this.

Returns:
Type Description
function - A function to invoke to stop listening to all of the events given.

inherited once(events, callback, context){function}

Listens for the first of the given events to be triggered and invokes the callback once.

Name Type Description
events String | Array

The event or events to listen to.

callback function

The function to invoke when any of the events are invoked.

context Object optional

The value of this when the callback is invoked. If not specified, the reference of the object this function exists on will be this.

Returns:
Type Description
function - A function to invoke to stop listening to all of the events given.

Creates a limited view of this collection known as a page. The resulting page object changes when this collection changes. At the very least the page size is required, and a starting page index can be specified.

Name Type Default Description
pageSize Number

The maximum number of elements allowed in the page at once.

pageIndex Number 0 optional

The starting page offset. This isn't an element offset, but the element offset can be calculated by multiplying the page index by the page size.

Returns:
Type Description
Rekord.Page - The newly created Page.

parseModel(input, remoteData){Rekord.Model}

Takes input provided to this collection for adding, removing, or querying and returns a model instance. An existing model can be referenced or a new model can be created on the spot.

Name Type Default Description
input modelInput

The input to convert to a model instance.

remoteData Boolean false optional

If the model is from a remote source. Remote sources place the model directly into the database while local sources aren't stored in the database until they're saved.

Returns:
Type Description
Rekord.Model - A model instance parsed from the input.

inherited pluck(values, keys, valuesDelim, keysDelim){Array|Object}

Plucks values from elements in the collection. If only a values property expression is given the result will be an array of resolved values. If the keys property expression is given, the result will be an object where the property of the object is determined by the key expression.

var c = Rekord.collect([{age: 2, nm: 'T'}, {age: 4, nm: 'R'}, {age: 5, nm: 'G'}]);
c.pluck(); // c
c.pluck('age'); // [2, 4, 5]
c.pluck('age', 'nm'); // {T: e, R: 4, G: 5}
c.pluck(null, 'nm'); // {T: {age: 2, nm: 'T'}, R: {age: 4, nm: 'R'}, G: {age: 5, nm: 'G'}}
c.pluck('{age}-{nm}'); // ['2-T', '4-R', '5-G']
Name Type Default Description
values propertyResolverInput optional

The expression which converts an element into a value to pluck.

keys propertyResolverInput optional

The expression which converts an element into an object property (key).

valuesDelim String ',' optional

A delimiter to use to join multiple value properties into a string.

keysDelim String ',' optional

A delimiter to use to join multiple key properties into a string.

See:
Returns:
Type Description
Array | Object - The plucked values.

Removes the last model in this collection and returns it - sorting the collection if a comparator is set on this collection and delaySort is no specified or a true value.

Name Type Default Description
delaySort Boolean false optional

Whether automatic sorting should be delayed until the user manually calls sort.

Fires:
Returns:
Type Description
Rekord.Model - The model removed from the end of the collection.

popWhere(dontDiscard, properties, value, equals){Rekord.ModelCollection}

Calls Rekord.Model#$pop on models in this collection that meet the given where expression.

Name Type Default Description
dontDiscard Boolean false optional

Whether to remove the saved state after the saved state has been applied back to the model. A falsy value will result in Rekord.Model#$discard being called.

properties whereInput optional

See Rekord.createWhere

value Any optional

See Rekord.createWhere

equals equalityCallback Rekord.equalsStrict optional

See Rekord.createWhere

See:
Returns:
Type Description
Rekord.ModelCollection - The reference to this collection.

Adds one or more models to the end of this collection - sorting the collection if a comparator is set on this collection.

Name Type Description
value modelInput repeatable

The models to add to this collection.

Fires:
Returns:
Type Description
Number - The new length of this collection.

pushWhere(fields, properties, value, equals){Rekord.ModelCollection}

Calls Rekord.Model#$push on models in this collection that meet the given where expression.

Name Type Default Description
fields Array.<String> optional

The set of fields to save for later popping or discarding. If not specified, all model fields will be saved.

properties whereInput optional

See Rekord.createWhere

value Any optional

See Rekord.createWhere

equals equalityCallback Rekord.equalsStrict optional

See Rekord.createWhere

See:
Returns:
Type Description
Rekord.ModelCollection - The reference to this collection.

Places a model in this collection providing a key to use.

Name Type Default Description
key modelKey

The key of the model.

model Rekord.Model

The model instance to place in the collection.

delaySort Boolean false optional

Whether automatic sorting should be delayed until the user manually calls sort.

Fires:
Returns:
Type Description
Rekord.ModelCollection - The reference to this collection.

Returns a random element in this collection.

Returns:
Type Description
Any - The randomly chosen element from this collection.

Rebuilds the internal index which maps keys to the index of the model in this collection.

Returns:
Type Description
Rekord.ModelCollection - The reference to this collection.

inherited reduce(reducer, initialValue){Any}

Reduces all the elements of this collection to a single value. All elements are passed to a function which accepts the currently reduced value and the current element and returns the new reduced value.

var reduceIt = function(curr, elem) {
 return curr + ( elem[0] * elem[1] );
};
var c = Rekord.collect([[2, 1], [3, 2], [5, 6]]);
c.reduce( reduceIt, 0 ); // 38
Name Type Description
reducer function

A function which accepts the current reduced value and an element and returns the new reduced value.

initialValue Any optional

The first value to pass to the reducer function.

Returns:
Type Description
Any - The reduced value.

refreshWhere(properties, value, equals){Rekord.ModelCollection}

Calls Rekord.Model#$refresh on models in this collection that meet the given where expression.

Name Type Default Description
properties whereInput optional

See Rekord.createWhere

value Any optional

See Rekord.createWhere

equals equalityCallback Rekord.equalsStrict optional

See Rekord.createWhere

See:
Returns:
Type Description
Rekord.ModelCollection - The reference to this collection.

remove(input, delaySort, equals){Rekord.Model}

Removes the given model from this collection if it exists - sorting the collection if a comparator is set on this collection and delaySort is not specified or a true value.

Name Type Default Description
input modelInput

The model to remove from this collection if it exists.

delaySort Boolean false optional

Whether automatic sorting should be delayed until the user manually calls sort.

equals equalityCallback Rekord.equalsStrict optional

The function which determines whether one of the elements that exist in this collection are equivalent to the given value.

Fires:
Returns:
Type Description
Rekord.Model - The element removed from this collection.

removeAll(inputs, delaySort){Array.<Rekord.Model>}

Removes the given models from this collection - sorting the collection if a comparator is set on this collection and delaySort is not specified or a true value.

Name Type Default Description
inputs Array.<modelInput>

The models to remove from this collection if they exist.

delaySort Boolean false optional

Whether automatic sorting should be delayed until the user manually calls sort.

Fires:
Returns:
Type Description
Array.<Rekord.Model> - The models removed from this collection.

Removes the model in this collection at the given index i - sorting the collection if a comparator is set on this collection and delaySort is not specified or a true value.

Name Type Default Description
i Number

The index of the model to remove.

delaySort Boolean false optional

Whether automatic sorting should be delayed until the user manually calls sort.

Fires:
Returns:
Type Description
Rekord.Model - The model removed, or undefined if the index was invalid.

removeWhere(callRemove, whereProperties, whereValue, whereEquals, out, delaySort){Array.<Rekord.Model>}

Removes the models from this collection where the given expression is true. The first argument, if true, can call Rekord.Model#$remove on each model removed from this colleciton.

Name Type Default Description
callRemove Boolean false optional

Whether Rekord.Model#$remove should be called on each removed model.

whereProperties whereInput optional

See Rekord.createWhere

whereValue Any optional

See Rekord.createWhere

whereEquals equalityCallback optional

See Rekord.createWhere

out Array this.cloneEmpty() optional

The array to place the elements that match.

delaySort Boolean false optional

Whether automatic sorting should be delayed until the user manually calls sort.

Fires:
Returns:
Type Description
Array.<Rekord.Model> - An array of models removed from this collection.

Resets the models in this collection with a new collection of models.

Name Type Default Description
models Array.<modelInput> optional

The initial array of models in this collection.

remoteData Boolean false optional

If the models array is from a remote source. Remote sources place the model directly into the database while local sources aren't stored in the database until they're saved.

See:
Fires:
Returns:
Type Description
Rekord.ModelCollection - The reference to this collection.

Reverses the order of models in this collection.

Fires:
Returns:
Type Description
Rekord.ModelCollection - The reference to this collection.

saveWhere(properties, value, equals, props, cascade){Rekord.ModelCollection}

Calls Rekord.Model#$save on models in this collection that meet the given where expression.

Name Type Default Description
properties whereInput optional

See Rekord.createWhere

value Any optional

See Rekord.createWhere

equals equalityCallback Rekord.equalsStrict optional

See Rekord.createWhere

props Object {} optional

Properties to apply to each model in the collection that pass the where expression.

cascade Number optional

Which operations should be performed out of: store, rest, & live.

See:
Returns:
Type Description
Rekord.ModelCollection - The reference to this collection.

inherited setComparator(comparator, nullsFirst){Rekord.Collection}

Sets the comparator for this collection and performs a sort.

Name Type Default Description
comparator ComparatorInput

The comparator input to convert to a comparison function.

nullsFirst Boolean false optional

When a comparison is done involving a null/undefined value this can determine which is ordered before the other.

See:
Fires:
Returns:
Type Description
Rekord.Collection

Removes the first model in this collection and returns it - sorting the collection if a comparator is set on this collection and delaySort is no specified or a true value.

var c = Rekord.collect(1, 2, 3, 4);
c.shift(); // 1
Name Type Default Description
delaySort Boolean false optional

Whether automatic sorting should be delayed until the user manually calls sort.

Fires:
Returns:
Type Description
Rekord.Model - The model removed from the beginning of the collection.

inherited sort(comparator, nullsFirst, ignorePrimitive){Rekord.Collection}

Sorts the elements in this collection based on the current comparator unless a comparator is given. If a comparator is given it will not override the current comparator, subsequent operations to the collection may trigger a sort if the collection has a comparator.

Name Type Default Description
comparator ComparatorInput optional

The comparator input to convert to a comparison function.

nullsFirst Boolean false optional

When a comparison is done involving a null/undefined value this can determine which is ordered before the other.

ignorePrimitive Boolean false optional

Sorting is automatically done for non-primitive collections if a comparator exists. This flag ensures primitive collections aren't sorted after every operation.

See:
Fires:
Returns:
Type Description
Rekord.Collection - The reference to this collection.

splice(start, deleteCount, values){Array.<Any>}

Splices elements out of and into this collection - sorting the collection if a comparator is set on this collection.

Name Type Description
start Number

Index at which to start changing the array (with origin 0). If greater than the length of the array, actual starting index will be set to the length of the array. If negative, will begin that many elements from the end.

deleteCount Number

An integer indicating the number of old array elements to remove. If deleteCount is 0, no elements are removed. In this case, you should specify at least one new element. If deleteCount is greater than the number of elements left in the array starting at start, then all of the elements through the end of the array will be deleted. If deleteCount is omitted, deleteCount will be equal to (arr.length - start).

values Any repeatable

The elements to add to the array, beginning at the start index. If you don't specify any elements, splice() will only remove elements from the array.

Fires:
Returns:
Type Description
Array.<Any> - The array of deleted elements.

inherited subtract(collection, out, equals){Array}

Returns a collection with elements that exist in this collection but does not exist in the given collection.

var a = Rekord.collect(1, 2, 3, 4);
var b = Rekord.collect(1, 3, 5);
var c = a.subtract( b ); // [2, 4]
Name Type Default Description
collection Array

The array of elements that shouldn't exist in the resulting collection.

out Array this.cloneEmpty() optional

The array to place the elements that exist in this collection but not in the given collection. If this is not given - a collection of this type will be created.

equals equalityCallback Rekord.equalsStrict optional

The function which determines whether one of the elements that exist in this collection are equivalent to an element that exists in the given collection.

Returns:
Type Description
Array - The collection of elements that exist in this collection and not the given collection.

Sums all numbers resolved from the given property expression and returns the result.

var c = Rekord.collect([2, 3, 4]);
c.sum(); // 9
var d = Rekord.collect([{age: 5}, {age: 4}, {age: 2}]);
d.sum('age'); // 11
Name Type Description
numbers propertyResolverInput optional

The expression which converts an element in this collection to a number.

See:
  • Rekord.createNumberResolver
Returns:
Type Description
Number - The sum of all valid numbers found in this collection.

Returns a copy of this collection as a plain Array.

Returns:
Type Description
Array - The copy of this collection as a plain array.

unshift(value){Number}

Name Type Description
value modelInput repeatable

The values to add to this collection.

See:
Fires:
Returns:
Type Description
Number - The new length of this collection.

update(props, value, remoteData, avoidSave, cascade){Rekord.ModelCollection}

Updates the given property(s) in all models in this collection with the given value. If avoidSave is not a truthy value then Rekord.Model#$save is called on every model in this collection.

Name Type Default Description
props String | Object

The property or properties to update.

value Any optional

The value to set if a String props is given.

remoteData Boolean false optional

If the properties are from a remote source. Remote sources place the model directly into the database while local sources aren't stored in the database until they're saved.

avoidSave Boolean false optional

True for NOT calling Rekord.Model#$save, otherwise false.

cascade Number optional

Which operations should be performed out of: store, rest, & live.

Fires:
Returns:
Type Description
Rekord.ModelCollection - The reference to this collection.

updateWhere(where, props, value, remoteData, avoidSave, cascade){Array.<Rekord.Model>}

Updates the given property(s) in models in this collection which pass the where function with the given value. If avoidSave is not a truthy value then Rekord.Model#$save is called on every model in this collection.

Name Type Default Description
where whereCallback

The function which determines whether a model should be updated.

props String | Object

The property or properties to update.

value * optional

The value to set if a String props is given.

remoteData Boolean false optional

If the properties are from a remote source. Remote sources place the model directly into the database while local sources aren't stored in the database until they're saved.

avoidSave Boolean false optional

True for NOT calling Rekord.Model#$save, otherwise false.

cascade Number optional

Which operations should be performed out of: store, rest, & live.

Fires:
Returns:
Type Description
Array.<Rekord.Model> - An array of models updated.

inherited where(whereProperties, whereValue, whereEquals, out){Rekord.Collection}

Creates a copy of this collection with elements that match the supplied parameters. The parameters are passed to the Rekord.createWhere to generate a function which tests each element of this collection for inclusion in the newly created collection.

var isEven = function() { return x % 2 == 0; };
var c = Rekord.collect(1, 2, 3, 4, 5);
var w = c.where(isEven); // [2, 4]
Name Type Default Description
whereProperties whereInput optional

See Rekord.createWhere

whereValue Any optional

See Rekord.createWhere

whereEquals equalityCallback optional

See Rekord.createWhere

out Array this.cloneEmpty() optional

The array to place the elements that match.

See:
Returns:
Type Description
Rekord.Collection - The copy of this collection ran through a filtering function.

Events

An event triggered when a single value is added to a collection.

Name Type Description
collection Rekord.Collection

The collection that triggered the event.

value T

The value added.

See:

An event triggered when multiple values are added to a collection.

Name Type Description
collection Rekord.Collection

The collection that triggered the event.

value Array.<T>

The values added.

See:

All events triggered by a collection when the contents of the collection changes.

Name Type Description
collection Rekord.Collection

The collection that triggered the event.

An event triggered when a collection is cleared of all elements.

Name Type Description
collection Rekord.Collection

The collection that triggered the event.

See:

An event triggered when a collection has an element removed at a given index.

Name Type Description
collection Rekord.Collection

The collection that triggered the event.

removing Any

The element that was removed.

index Number

The index where the element was removed at.

See:

An event triggered when a collection has multiple elements removed.

Name Type Description
collection Rekord.Collection

The collection that triggered the event.

removed Array.<Any>

The array of elements removed from the collection.

See:

An event triggered when a collection's elements are entirely replaced by a new set of elements.

Name Type Description
collection Rekord.Collection

The collection that triggered the event.

updated Array

The array of elements modified.

See:

An event triggered when a collection is sorted. This may automatically be triggered by any method that modifies the collection.

Name Type Description
collection Rekord.Collection

The collection that triggered the event.

See:

An event triggered when a collection has elements modified.

Name Type Description
collection Rekord.Collection

The collection that triggered the event.

updated Array

The array of elements modified.

See: