Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions js/collections/adaptCollection.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,19 @@
/**
* @file AdaptCollection - Base Backbone collection for Adapt data
* @module core/js/collections/adaptCollection
* @description Base collection class for Adapt Framework data sets. Fires
* `adaptCollection:dataLoaded` on its first reset, signalling downstream
* modules that the collection's data is available.
*/
import Adapt from 'core/js/adapt';

/**
* @class AdaptCollection
* @classdesc Base Backbone collection used throughout the Adapt Framework.
* Fires `adaptCollection:dataLoaded` on its first reset, allowing dependent
* modules to react when the collection's data is available.
* @extends {Backbone.Collection}
*/
export default class AdaptCollection extends Backbone.Collection {

initialize(models, options) {
Expand Down
21 changes: 21 additions & 0 deletions js/collections/adaptSubsetCollection.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,21 @@
/**
* @file AdaptSubsetCollection - Filtered subset of a parent AdaptCollection
* @module core/js/collections/adaptSubsetCollection
* @description A filtered view of a parent {@link module:core/js/collections/adaptCollection}.
* Automatically re-filters its contents whenever the parent collection resets,
* keeping only models that are instances of this collection's model type.
*/
import AdaptCollection from 'core/js/collections/adaptCollection';

/**
* @class AdaptSubsetCollection
* @classdesc Maintains a live, filtered subset of a parent
* {@link module:core/js/collections/adaptCollection|AdaptCollection}. When the parent
* resets, `loadSubset` rebuilds the subset by retaining only models that are
* instances of `this.model`. Also builds a `_byAdaptID` lookup map for fast
* retrieval by `_id`.
* @extends {AdaptCollection}
*/
export default class AdaptSubsetCollection extends AdaptCollection {

initialize(models, options) {
Expand All @@ -8,6 +24,11 @@ export default class AdaptSubsetCollection extends AdaptCollection {
this.listenTo(this.parent, 'reset', this.loadSubset);
}

/**
* Rebuilds the subset from the parent collection, keeping only models that
* are instances of `this.model`. Also indexes the subset by `_id` in
* `this._byAdaptID` for fast lookup.
*/
loadSubset() {
this.set(this.parent.filter(model => model instanceof this.model));
this._byAdaptID = this.groupBy('_id');
Expand Down
Loading
Loading