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:
- Source:
Methods
Parameters:
- 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:
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:
- 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:
Parameters:
- 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:
- 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:
- Source:
Returns:
-
Type
-
integer