Collection

Collection

new Collection(data, model, (indexBy))

Collection is a dictionary helper class. A Collection requires a model to maintain consistency and has methods that allow the addition, removal, and getting access to Models.

Source:
Parameters:
Name Type Description
data Object | Array

data to create a collection with on construction. data can be passed after instantiation

model Model

the Model to be used when adding data to a collection. A Collection will force all data into this Model.

(indexBy) String

[id] - the field that the collection should index on

Extends

Members

count

returns the number of items in the Collection

Source:
Example
assert(pokemonCollection.count === 1); // true

Methods

add(model) → {Collection}

A method that adds a model to the index. This method will throw if the same index key already exists

Source:
Parameters:
Name Type Description
model Object | Model
Throws:
Error
Returns:
Type:
Collection
Example
pokemonCollection.add({
   name: 'Ditto'
});

assert(pokemonCollection.count === 2); // true

clear() → {Collection}

clears all items in the index

Source:
Returns:
Type:
Collection

contains((id)) → {Boolean}

if id is passed in returns true if the specific model exists otherwise returns true if the collection has > 0 models

Source:
Parameters:
Name Type Description
(id) String
Returns:
Type:
Boolean

get((id)) → {Object|Model|Array.<(Object|Model)>}

Get a model by it's index key or get the entire collection (by passing no params). By not passing an id the call is synonymous with calling the .toArray() on an instance.

Source:
Parameters:
Name Type Description
(id) String
Returns:
Type:
Object | Model | Array.<(Object|Model)>
Example
const ditto = pokemonCollection.get('Ditto');
assert(ditto === pokemonCollection.index['Ditto']); // true

remove(modelId) → {Collection}

Remove a model by it's index key (the property that has been set to ne indexed in the models constructor)

Source:
Parameters:
Name Type Description
modelId String
Returns:
Type:
Collection
Example
pokemonCollection.remove('Mewtwo');

assert(pokemonCollection.count === 1); //true
assert(pokemonCollection.get('Mewtwo') === null); // true

set(data) → {Collection}

A method that clears the index and sets the collection index again from an array of models or just a model.

Source:
Parameters:
Name Type Description
data Object | Array
Throws:
TypeError
Returns:
Type:
Collection
Example
pokemonCollection.set({
  name: 'Pikachu'
});

assert(pokemonCollection.count === 1); // true

toArray() → {Array.<Object>}

returns the index as an array

Source:
Returns:
Type:
Array.<Object>

toJSON() → {String}

converts index to an array and stringifies

Overrides:
Source:
Returns:
Type:
String

toObject() → {Object}

converts a class to an object

Inherited From:
Source:
Returns:
Type:
Object

update(model) → {Collection}

Updates a model in the index by it's index key (in the example case 'name') and overwrites all field data

Source:
Parameters:
Name Type Description
model Object | Model
Throws:
Error
Returns:
Type:
Collection
Example
const quote = 'Ditto!';
pokemonCollection.update({
  name: 'Ditto',
  quote
});

assert(pokemonCollection.quote === quote); // true