A collection is a list of models. See the class.
Fetches a collection (from the back-end). Returns a promise.
collection.fetch();
collection.fetch({
remove: false,
more: true,
});
Options:
- remove – false will prevent removing current models;
- more – load next portion of records (show-more).
You can pass custom options and check them in 'change' event listeners.
Clone a collection. Models are kept identical. Returns a collection instance.
Get a model from a collection by an id.
const model = collection.get(id);
Get a model by its position in a collection.
const model = collection.at(index);
Iterates through a collection.
collection.forEach(model => {});
Add a model or multiple models to a collection. Firing an 'add' event for each model, and an 'update' event afterwards.
collection.add(model);
collection.add([model]);
Remove a model from a collection. Fires a 'remove' event for a model, and an 'update' event afterwards.
collection.remove(id);
collection.remove(model, {
silent: true, // Suppresses events.
});
Empties a collection.
Add a model to the end.
Remove and return the last model.
Add a model at the beginning.
Remove and return the first model.
Resets order parameters to default.
Sets order parameters.
@param {string|null} orderBy
@param {boolean|'asc'|'desc'|null} [order]
@param {boolean} [setDefault]
Abort the last fetch.
Whether more records can be fetched.
Set an offset.
collection.setOffset(offset);
Get a where-clause.
string
An entity type.
number
A current length of a collection.
number
A total number of records in the backend.
number
A current offset (for pagination).
number
A max size to fetch (for pagination).
Object
A where clause.
With a factory:
define('custom:views/some-custom-view', ['view'], (View) => {
return class extends View {
setup() {
this.wait(this.loadCollection());
}
async loadCollection() {
this.collection = await this.getCollectionFactory().create('Account');
await this.collection.fetch();
}
}
});
Without factory:
define('custom:views/some-custom-view', ['view', 'collection'], (View, Collection) => {
return class extends View {
setup() {
const collection = new Collection();
// The URL will be used when fetching from the backend.
collection.url = 'MyEntityType/someEndPoint';
this.wait(
collection.fetch()
);
}
}
});
Note: listenTo
and listenToOnce
are methods of the view class.
Once a collection is fetched.
this.listenTo(collection, 'sync', (collection, response, options) => {});
Once a collection is updated.