The track() method creates a new collection that is automatically updated as the parent collection changes. Tracked collections include index and previousIndex in their events to indicate the position of the change.
Requirements:
track() is typically provided by dstore/Trackable.- For server-side stores (like
Rest), you must mix in SimpleQuery to provide client-side querying capabilities required by Trackable.
Usage Example:
// Setup a trackable store
var TrackableMemory = declare([Memory, Trackable]);
var store = new TrackableMemory({ data: [...] });
// Track a filtered/sorted subset
var filteredSorted = store.filter({ inStock: true }).sort('price');
var tracked = filteredSorted.track();
// Listen for changes with index information
tracked.on('add, update, delete', function (event) {
var newIndex = event.index;
var oldIndex = event.previousIndex;
var object = event.target;
});
Key Behaviors:
- Indices reported are relative to the entire collection, not just the fetched range.
- If an object is added/updated outside of currently fetched ranges,
index will be undefined. - If an object falls between fetched ranges,
beforeIndex is provided to indicate the index of the first object the new/updated object comes before.
// Example of creating a trackable Rest store
var TrackableRest = declare([Rest, SimpleQuery, Trackable]);
var store = new TrackableRest({ ... });
var tracked = store.filter({ category: 'books' }).track();
tracked.on('add', function(event) {
console.log('Added at index:', event.index);
});