    import CollectionShadow from 'f.lux/src/collection/CollectionShadow.js'

public class | source

# CollectionShadow

#### Extends:

Shadow → CollectionShadow

Shadow api for the `CollectionProperty`.

## Basic functions

- `add(state, mergeOp)`
- `all()`
- `clear()</li> <li>`create(model)`</li> <li>`destroy(id)`</li> <li>`fetch(filter, mergeOp)`</li> <li>`find(id)`</li> <li>`get(id)`</li> <li>`has(id)`</li> <li>`remove(id)`

## Utility functions

- `entries()`
- `every(iteratee, context)`
- `filter(iteratee, context)`
- `groupBy(callback, context)`
- `keys()`
- `map(iteratee, context)`
- `reduce(iteratee, acc, context)`
- `some(iteratee, context)`
- `sortBy(...iteratee)`
- `values()`

## Paging function

- `fetchNext()` - gets the next group of models
- `isPaging()` - gets if a paging request is outstanding (only one paging request per collection allowed at one time)
- `hasMorePages()` - does the endpoint have additional models
- `nextOffset()` - gets the offset for the next paging request
- `resetPaging()` - resets the internal paging variables
- `setLimit()` - change the number of models requested with each `fetchNext()` request

#### See:

- CollectionProperty

## Constructor Summary

Public Constructor
public | constructor(impl: *) |

## Member Summary

Public Members
public | [_cache]: {} |
public get | endpoint: Object: * Gets the endpoint for accessing the remote data source. |
public get | fetching: * Gets if a fetching operation that will replace ALL models is in progress. |
public get | id: string: * Gets the endpoint ID, which for RestEndpointProperty is its URL. |
public get | limit: * Gets the number of models to fetch with each paging request. |
public get | nextOffset: * Gets the offset for the next paging request. |
public get | paging: * Gets if a paging operation (CollectionProperty#fetchNext) call is in progress. |
public get | size: * Gets the number of models being managed by the collection |

## Method Summary

Public Methods
public | [Symbol.iterator](): * |
public | add(state: object, mergeOp: string): * Synchronously adds a new model object to the collection. |
public | addModel(state: *, mergeOp: *): * |
public | addModels(models: array, mergeOp: string, syncOp: boolean) Bulk adds multiple models. |
public | all(): array Gets the shadow models currently managed by the collection. |
public | clear() Removes all models from the collection and marks the collection as having not synched with the endpoint. |
public | create(model: Object): Promise Combines an CollectionShadow#add and CollectionShadow#save actions. |
public | destroy(id: *): Promise Permantently deletes the model with the endpoint and removes it from the collection. |
public | entries(): Array Gets an iterator with [ID, model] paris. |
public | every(iteratee: *, context: *): boolean Tests whether all elements in the array pass the test implemented by the provided function. |
public | fetch(filter: *, mergeOp: string): Promise Fetches all the models from the endpoint. |
public | fetchNext(mergeOp: string): Promise Fetches the next set of models from the endpoint. |
public | filter(iteratee: *, context: *): * Creates an array with all elements that pass the test implemented by the provided function. |
public | find(id: *): Promise Gets a model by ID. |
public | findModel(iteratee: *, context: object): * Gets the first model that satisfies the provided testing function. |
public | forEach(iteratee: *, context: object) Executes a provided function once for each array element. |
public | get(id: *): Object Gets a model by ID. |
public | groupBy(callback: *, context: *) Inspired by Lodash groupBy() method. |
public | has(id: *): boolean Gets if the collection contains a matching model. |
public | hasMorePages(): * Gets if additional paging calls will return additional results. |
public | isAutocheckpoint(): boolean Sets whether auto-checkpointing is enabled. |
public | isConnected(): * Gets if the collection is active (has a shadow) and an endpoint. |
public | isNew(id: *): * Gets if a model has never been persisted to the endpoint. |
public | keys(): Iterator Gets an iterator containing each model IDs. |
public | keysArray(): Array Gets an array containing each model iDs. |
public | map(iteratee: *, context: object): * Creates a new array with the results of calling a provided function on every element in this collection. |
public | reduce(iteratee: *, acc: *, context: object): * Applies a function against an accumulator and each value of the collection to reduce it to a single value. |
public | remove(id: *): * Synchronously removes the model from the collection without performing an endpoint operation. |
public | resetToLastEndpointState() Resets all dirty models to their last checkpoint and removes all unsaved models. |
public | setAutocheckpoint(auto: boolean) Sets whether auto-checkpointing should be enabled. |
public | setEndpoint(endPoint: *) Sets the CollectionProperty#endpoint. |
public | setLimit(limit: number) Sets the number of models to request with each paging request. |
public | setModels(models: array) Bulk replaces current models with an array of new models. |
public | some(iteratee: *, context: object): boolean Tests whether some element in the array passes the test implemented by the provided function. |
public | sortBy(iteratee: ...*): * |
public | values(): Iterator Gets an iterator containing each f.lux shadow state model. |
public | valuesArray(): Array Gets an array containing each model iDs. |

## Inherited Summary

From class Shadow
public | [_impl]: * |
public | $(): Access Gets the Access object for obtaining information about a property. |
public | $$(): Property Gets the Property managing this shadow state property. |
public | toJSON(): Object | Array Gets the actual state property being proxied by this shadow state property. |
public | toString(): * |

## Public Constructors

### public constructor(impl: *) source

#### Override:

Shadow#constructor

#### Params:

Name | Type | Attribute | Description
impl | * |  |

## Public Members

### public [_cache]: {} source

### public get endpoint: Object: * source

Gets the endpoint for accessing the remote data source.

#### Return:

Object

### public get fetching: * source

Gets if a fetching operation that will replace ALL models is in progress.

### public get id: string: * source

Gets the endpoint ID, which for RestEndpointProperty is its URL.

#### Return:

string

### public get limit: * source

Gets the number of models to fetch with each paging request.

### public get nextOffset: * source

Gets the offset for the next paging request.

### public get paging: * source

Gets if a paging operation (CollectionProperty#fetchNext) call is in progress.

### public get size: * source

Gets the number of models being managed by the collection

## Public Methods

### public [Symbol.iterator](): * source

#### Return:

*

### public add(state: object, mergeOp: string): * source

Synchronously adds a new model object to the collection. Call `model.$().save()` to persist the newly added object.

#### Params:

Name | Type | Attribute | Description
state | object |  | the json model to add to the collection

mergeOp

string

- optional
- default: REPLACE_OPTION

one of `DEFAULTS_OPTION`, `MERGE_OPTION`, or `REPLACE_OPTION` and specifies how to combine an existing model with a matching ID with a newly retrieved model.

#### Return:

* | the object's ID. And ID is assigned if the 'id' parameter was not set and it could not be found in the `state` parameter.

### public addModel(state: *, mergeOp: *): * source

#### Params:

Name | Type | Attribute | Description
state | * |  |

mergeOp

*

- optional
- default: REPLACE_OPTION

#### Return:

*

### public addModels(models: array, mergeOp: string, syncOp: boolean) source

Bulk adds multiple models. Models must have an ID as it is assumed they have been previously saved.

#### Params:

Name | Type | Attribute | Description
models | array |  | array of model values

mergeOp

string

- optional
- default: REPLACE_OPTION

one of `DEFAULTS_OPTION`, `MERGE_OPTION`, or `REPLACE_OPTION` and specifies how to combine an existing model with a matching ID with a newly retrieved model.

syncOp

boolean

- optional
- default: true

sets the `synced` flag to true if this parameter is true

### public all(): array source

Gets the shadow models currently managed by the collection.

#### Return:

array | all models being managed by the collection

### public clear() source

Removes all models from the collection and marks the collection as having not synched with the endpoint.

### public create(model: Object): Promise source

Combines an CollectionShadow#add and CollectionShadow#save actions.

#### Params:

Name | Type | Attribute | Description
model | Object |  | the json data

#### Return:

Promise | resolves with the f.lux shadow state for the new model

### public destroy(id: *): Promise source

Permantently deletes the model with the endpoint and removes it from the collection.

#### Params:

Name | Type | Attribute | Description
id | * |  | the model ID to delete

#### Return:

Promise | resolves with the ID for the removed mdoel

### public entries(): Array source

Gets an iterator with [ID, model] paris.

#### Return:

Array

### public every(iteratee: *, context: *): boolean source

Tests whether all elements in the array pass the test implemented by the provided function.

#### Params:

Name | Type | Attribute | Description
iteratee | * |  |
context | * |  |

#### Return:

boolean

### public fetch(filter: *, mergeOp: string): Promise source

Fetches all the models from the endpoint.

#### Params:

Name | Type | Attribute | Description

filter

*

- optional
- default: null

filter object created using the endpoint.

mergeOp

string

- optional
- default: REPLACE_OPTION

one of `DEFAULTS_OPTION`, `MERGE_OPTION`, or `REPLACE_OPTION` and specifies how to combine an existing model with a matching ID with a newly retrieved model.

#### Return:

Promise | resolves with the json models from the endpoint

### public fetchNext(mergeOp: string): Promise source

Fetches the next set of models from the endpoint. This method differs from CollectionProperty#fetch by passing `offset` and `limit` filter criteria to the endpoint.

#### Params:

Name | Type | Attribute | Description

mergeOp

string

- optional
- default: REPLACE_OPTION

one of `DEFAULTS_OPTION`, `MERGE_OPTION`, or `REPLACE_OPTION` and specifies how to combine an existing model with a matching ID with a newly retrieved model.

#### Return:

Promise | resolves with a single argument of an array of model json objects as returned from the endpoint.

### public filter(iteratee: *, context: *): * source

Creates an array with all elements that pass the test implemented by the provided function.

#### Params:

Name | Type | Attribute | Description
iteratee | * |  |
context | * |  |

#### Return:

*

### public find(id: *): Promise source

Gets a model by ID. The method looks for a matching model in the collection. If one is not found then one is requested from the endpoint.

#### Params:

Name | Type | Attribute | Description
id | * |  | the model ID

#### Return:

Promise | reolves with the model or undefined if one is not found

### public findModel(iteratee: *, context: object): * source

Gets the first model that satisfies the provided testing function.

#### Params:

Name | Type | Attribute | Description
iteratee | * |  | Function to execute on each value in the array, taking three arguments: element, ID, and the collection reference.

context

object

- optional

object to use as `this` when executing callback

#### Return:

* | the model or undefined if one is not found

### public forEach(iteratee: *, context: object) source

Executes a provided function once for each array element.

#### Params:

Name | Type | Attribute | Description
iteratee | * |  | Function to execute on each value in the array, taking three arguments: element, ID, and the collection reference.

context

object

- optional

object to use as `this` when executing callback

### public get(id: *): Object source

Gets a model by ID. The method looks for a matching model in the collection and never looks for one remotely through the endpoint.

#### Params:

Name | Type | Attribute | Description
id | * |  | the model ID

#### Return:

Object | the model or undefined if one is not found

### public groupBy(callback: *, context: *) source

Inspired by Lodash groupBy() method.

Creates an object composed of keys generated from the results of running each element of collection thru iteratee. The order of grouped values is determined by the order they occur in collection. The corresponding value of each key is an array of elements responsible for generating the key. The iteratee is invoked with one argument: (value).

#### Params:

Name | Type | Attribute | Description
callback | * |  |
context | * |  |

#### See:

- https://lodash.com/docs/4.17.4#groupBy

### public has(id: *): boolean source

Gets if the collection contains a matching model.

#### Params:

Name | Type | Attribute | Description
id | * |  | the model ID

#### Return:

boolean

### public hasMorePages(): * source

Gets if additional paging calls will return additional results.

#### Return:

*

### public isAutocheckpoint(): boolean source

Sets whether auto-checkpointing is enabled.

#### Return:

boolean

### public isConnected(): * source

Gets if the collection is active (has a shadow) and an endpoint.

#### Return:

*

### public isNew(id: *): * source

Gets if a model has never been persisted to the endpoint.

#### Params:

Name | Type | Attribute | Description
id | * |  |

#### Return:

*

### public keys(): Iterator source

Gets an iterator containing each model IDs.

#### Return:

Iterator

### public keysArray(): Array source

Gets an array containing each model iDs.

#### Return:

Array

### public map(iteratee: *, context: object): * source

Creates a new array with the results of calling a provided function on every element in this collection.

#### Params:

Name | Type | Attribute | Description
iteratee | * |  | Function that produces an element of the new array, taking three arguments: element, ID, and the collection reference.

context

object

- optional

object to use as `this` when executing callback

#### Return:

*

### public reduce(iteratee: *, acc: *, context: object): * source

Applies a function against an accumulator and each value of the collection to reduce it to a single value.

#### Params:

Name | Type | Attribute | Description
iteratee | * |  | Function to execute on each value in the array, taking four arguments: accumulator, element, ID, and the collection reference.
acc | * |  | the initial accumulator value

context

object

- optional

object to use as `this` when executing callback

#### Return:

*

### public remove(id: *): * source

Synchronously removes the model from the collection without performing an endpoint operation.

#### Params:

Name | Type | Attribute | Description
id | * |  | the model id or cid

#### Return:

*

### public resetToLastEndpointState() source

Resets all dirty models to their last checkpoint and removes all unsaved models.

### public setAutocheckpoint(auto: boolean) source

Sets whether auto-checkpointing should be enabled.

#### Params:

Name | Type | Attribute | Description
auto | boolean |  | `true` to enable.

### public setEndpoint(endPoint: *) source

Sets the CollectionProperty#endpoint.

#### Params:

Name | Type | Attribute | Description
endPoint | * |  |

### public setLimit(limit: number) source

Sets the number of models to request with each paging request. The default paging size is 50.

#### Params:

Name | Type | Attribute | Description
limit | number |  |

### public setModels(models: array) source

Bulk replaces current models with an array of new models.

#### Params:

Name | Type | Attribute | Description
models | array |  | array of model values

### public some(iteratee: *, context: object): boolean source

Tests whether some element in the array passes the test implemented by the provided function.

#### Params:

Name | Type | Attribute | Description
iteratee | * |  | Function to test for each element, taking three arguments: element, ID, and the collection reference.

context

object

- optional

object to use as `this` when executing callback

#### Return:

boolean

### public sortBy(iteratee: ...*): * source

#### Params:

Name | Type | Attribute | Description
iteratee | ...* |  |

#### Return:

*

### public values(): Iterator source

Gets an iterator containing each f.lux shadow state model.

#### Return:

Iterator

### public valuesArray(): Array source

Gets an array containing each model iDs.

#### Return:

Array