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
|
[Object] - 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(personCollection.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
personCollection.add({
name: 'Denzel Washington'
});
assert(personCollection.count === 2); // true
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
|
Example
const denzel = personCollection.get('Denzel Washington');
assert(denzel === personCollection.index['Denzel Washington']); // 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
personCollection.remove('Ethan Hawke');
assert(personCollection.count === 1); //true
assert(personCollection.get('Ethan Hawke') === 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
personCollection.set({
name: 'Ethan Hawke'
});
assert(personCollection.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 = 'My Man!';
personCollection.update({
name: 'Denzel Washington',
quote
});
assert(personCollection.quote === quote); // true