From e0afbc20e411590adeff9eefc94da785eca34ddc Mon Sep 17 00:00:00 2001 From: Eugene Voityuk Date: Thu, 11 May 2017 15:24:15 +0300 Subject: [PATCH 1/3] Collections model props as deps in derived. Allows specifying collection's model property as the dependency in derived property. So if given property will change in any of the collection's models, derived will be re-calculated. Introduces private property _collectionEvents, to store already subscribed events, and to do not subscribe twice for the same event in case if multiple derived properties will use the same dependency. --- ampersand-state.js | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/ampersand-state.js b/ampersand-state.js index af76b99..aa5001a 100644 --- a/ampersand-state.js +++ b/ampersand-state.js @@ -460,6 +460,19 @@ assign(Base.prototype, Events, { def.deps.forEach(function (propString) { self._keyTree.add(propString, update); + if (propString.indexOf('.') > -1) { + var collection = propString.split('.')[0]; + if (self[collection] && self[collection].isCollection) { + if (!self._collectionEvents[propString]) { + self._collectionEvents[propString] = true; + self[collection].on('change:' + propString.split('.')[1], function () { + self._keyTree.get(propString).forEach(function (fn) { + fn(); + }); + }); + } + } + } }); }); From 11a12603e3d3266e5fd70a0c497766597d4d5e72 Mon Sep 17 00:00:00 2001 From: Eugene Voityuk Date: Thu, 11 May 2017 16:19:27 +0300 Subject: [PATCH 2/3] Added ability to specify general events (all,add,remove) --- ampersand-state.js | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/ampersand-state.js b/ampersand-state.js index aa5001a..1235471 100644 --- a/ampersand-state.js +++ b/ampersand-state.js @@ -472,6 +472,18 @@ assign(Base.prototype, Events, { }); } } + } else if (propString.indexOf(':') > -1) { + var collection = propString.split(':')[0]; + if (self[collection] && self[collection].isCollection) { + if (!self._collectionEvents[propString]) { + self._collectionEvents[propString] = true; + self[collection].on(propString.split(':')[1], function () { + self._keyTree.get(propString).forEach(function (fn) { + fn(); + }); + }); + } + } } }); }); From 51c296bde5e8022bceeffa6f8d39c37dec5ce369 Mon Sep 17 00:00:00 2001 From: Eugene Voityuk Date: Thu, 11 May 2017 18:06:30 +0300 Subject: [PATCH 3/3] Added missed property --- ampersand-state.js | 1 + 1 file changed, 1 insertion(+) diff --git a/ampersand-state.js b/ampersand-state.js index 1235471..53c5364 100644 --- a/ampersand-state.js +++ b/ampersand-state.js @@ -36,6 +36,7 @@ function Base(attrs, options) { this._initCollections(); this._initChildren(); this._cache = {}; + this._collectionEvents = {}; this._previousAttributes = {}; if (attrs) this.set(attrs, assign({silent: true, initial: true}, options)); this._changed = {};