From 51f4578a417198fd6a0e72995b724f5bc5048ad0 Mon Sep 17 00:00:00 2001 From: Julien Fontanet Date: Tue, 14 Apr 2015 10:58:58 +0200 Subject: [PATCH] View handles existing items. --- packages/xo-collection/src/view.js | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/packages/xo-collection/src/view.js b/packages/xo-collection/src/view.js index a0ee74d69..358fe74cb 100644 --- a/packages/xo-collection/src/view.js +++ b/packages/xo-collection/src/view.js @@ -13,6 +13,9 @@ export default class View extends Collection { this._collection = collection this._predicate = createCallback(predicate, thisArg) + // Handles initial items. + this._onAdd(this._collection.all) + // Bound versions of listeners. this._onAdd = bind(this._onAdd, this) this._onUpdate = bind(this._onUpdate, this) @@ -24,6 +27,8 @@ export default class View extends Collection { this._collection.on('remove', this._onRemove) } + // This method is necessary to free the memory of the view if its + // life span is shorter than the collection. destroy () { this._collection.removeListener('add', this._onAdd) this._collection.removeListener('update', this._onUpdate) @@ -51,7 +56,10 @@ export default class View extends Collection { forEach(items, (value, key) => { if (predicate(value, key, this)) { - super.add(key, value) + // super.add() cannot be used because the item may already be + // in the view if it was already present at the creation of + // the view and its event not already emitted. + super.set(key, value) } }) }