Class: DataCollection

DataCollection(dataopt, optionsopt)

An ordered list of DataModel instances, either locally or [fetched](https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API) from a service. There are various ways to sort the collection, from DataCollection.sort to DataCollection.keepSortedByField. The comparator functions used in sorting should use the same return values (e.g. -1, 0, 1) as the [Array.prototype.sort](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort) comparators.

Constructor

new DataCollection(dataopt, optionsopt)

Parameters:
Name Type Attributes Default Description
data Array.<Object> <optional>
[] An array of data objects that are loaded into DataModels
options Object <optional>
{}
Properties
Name Type Attributes Default Description
dataObject class <optional>
DataModel the `class` of a `DataObject` type to use to wrap each data item in this collection
Source:
Examples

Constructed with data:

this.collection = new DataCollection([
	{ id: 0, title: 'first model'},
	{ id: 1, title: 'second model'},
	{ id: 3, title: 'third model'}
])

A custom class that is populated from a service:

class ExampleCollection extends Collection {
	get url() { return '/api/examples' }
}
this.collection = new ExampleCollection()
this.collection.fetch().then(() => { ... }).catch((err) => { ... })

Members

dataObjects :Array.<DataObject>

Type:
Source:

length :int

The number of data items in this collection
Type:
  • int
Source:

Methods

add(dataObject) → {DataCollection}

Parameters:
Name Type Description
dataObject DataObject
Source:
Returns:
- returns `this` for easy chaining
Type
DataCollection

addBatch(dataObjects)

Add an array of DataObjects to the end of the collection
Parameters:
Name Type Description
dataObjects Array.<DataObject>
Source:

at(index) → {DataObject}

Parameters:
Name Type Description
index int
Source:
Throws:
throw when index is out of range
Type
Error
Returns:
the DataObject at `index` in the internal list
Type
DataObject

create(data, optionsopt) → {Promise.<DataModel, Error>}

Parameters:
Name Type Attributes Default Description
data Object the data used to create a new DataModel in this collection
options Object <optional>
{} the options passed into DataModel.constructor
Source:
Returns:
Type
Promise.<DataModel, Error>

firstByField(dataField, value) → {DataObject|null}

Find the first DataModel with a certain field value.
Parameters:
Name Type Description
dataField string The name of the DataModel field in which to look
value * The value of the field to match using `===`
Source:
Returns:
The first matching DataModel or null if there is no match
Type
DataObject | null
Example
this.collection = new DataCollection(...)
this.collection.fetch().then(() => {
	console.log('DataModel with id 42:', this.collection.firstByField('id', 42)
})


	

indexOf(dataObject) → {number}

Parameters:
Name Type Description
dataObject DataObject
Source:
Returns:
// the positive index integer or -1
Type
number

keepSortedByField(dataField, comparatoropt)

Parameters:
Name Type Attributes Default Description
dataField string
comparator DataCollection~comparator <optional>
DataCollection.defaultComparator
Source:

remove(dataObject) → {DataCollection}

Parameters:
Name Type Description
dataObject DataObject
Source:
Returns:
returns `this` (the collection) for easy chaining
Type
DataCollection

reset(data)

Reset the state of the collection.
Parameters:
Name Type Description
data Array.<Object> Used like the `data` parameter of the contructor to reset the state of the collection
Source:

sort(comparator)

Rearranges the order of the Collection using a specific sorting algorithm
Parameters:
Name Type Description
comparator DataCollection~comparator
Source:

sortByAttribute(attributeName, comparatoropt)

Parameters:
Name Type Attributes Default Description
attributeName string
comparator DataCollection~comparator <optional>
DataCollection.defaultComparator
Source:

stringify() → {string}

Source:
Returns:
a JSON-formatted version of this collection's data
Type
string

Type Definitions

comparator(dataObject1, dataObject2) → {integer}

Parameters:
Name Type Description
dataObject1 DataObject
dataObject2 DataObject
Source:
Returns:
Type
integer