From a7d9c76f5a8d95ef8be42176c6c267cbcc58da65 Mon Sep 17 00:00:00 2001 From: Cristi Scheye Date: Tue, 10 Feb 2015 17:05:50 -0800 Subject: [PATCH 1/3] use deep quality comparison for derived updates --- ampersand-state.js | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/ampersand-state.js b/ampersand-state.js index 86ce52e..50ac159 100644 --- a/ampersand-state.js +++ b/ampersand-state.js @@ -411,8 +411,19 @@ assign(Base.prototype, BBEvents, { options = options || {}; var newVal = def.fn.call(self); + var newValToCompare, cachedValToCompare; - if (self._cache[name] !== newVal || !def.cache) { + // in case the data has a toJSON val, e.g. a Model, use that for comparison + if (newVal.toJSON && self._cache[name].toJSON) { + newValToCompare = newVal.toJSON(); + cachedValToCompare = self._cache[name].toJSON(); + } + else { + newValToCompare = newVal; + cachedValToCompare = self._cache[name]; + } + + if (!_.isEqual(newValToCompare, cachedValToCompare) || !def.cache) { if (def.cache) { self._previousAttributes[name] = self._cache[name]; } From 6226c6e7bf6321f350fbd75d8aeef30445f896b7 Mon Sep 17 00:00:00 2001 From: Cristi Scheye Date: Tue, 24 Mar 2015 14:45:30 -0700 Subject: [PATCH 2/3] using amp-is-object-equal module for comparison --- ampersand-state.js | 16 ++++------------ 1 file changed, 4 insertions(+), 12 deletions(-) diff --git a/ampersand-state.js b/ampersand-state.js index 50ac159..4f6ec48 100644 --- a/ampersand-state.js +++ b/ampersand-state.js @@ -25,6 +25,7 @@ var BBEvents = require('backbone-events-standalone'); var KeyTree = require('key-tree-store'); var arrayNext = require('array-next'); var changeRE = /^change:/; +var isObjectEqual = require('amp-is-object-equal'); function Base(attrs, options) { options || (options = {}); @@ -410,20 +411,11 @@ assign(Base.prototype, BBEvents, { var update = function (options) { options = options || {}; - var newVal = def.fn.call(self); - var newValToCompare, cachedValToCompare; - // in case the data has a toJSON val, e.g. a Model, use that for comparison - if (newVal.toJSON && self._cache[name].toJSON) { - newValToCompare = newVal.toJSON(); - cachedValToCompare = self._cache[name].toJSON(); - } - else { - newValToCompare = newVal; - cachedValToCompare = self._cache[name]; - } - if (!_.isEqual(newValToCompare, cachedValToCompare) || !def.cache) { + var newVal = def.fn.call(self); + + if (!isObjectEqual(self._cache[name], newVal) || !def.cache) { if (def.cache) { self._previousAttributes[name] = self._cache[name]; } From eb47ccb343fd031e04543c05df8cbf66580750c3 Mon Sep 17 00:00:00 2001 From: Cristi Scheye Date: Tue, 24 Mar 2015 15:26:57 -0700 Subject: [PATCH 3/3] adding module to package.json --- package.json | 1 + 1 file changed, 1 insertion(+) diff --git a/package.json b/package.json index 76feb90..52dd355 100644 --- a/package.json +++ b/package.json @@ -13,6 +13,7 @@ }, "dependencies": { "ampersand-version": "^1.0.0", + "amp-is-object-equal": "^1.0.2", "array-next": "~0.0.1", "backbone-events-standalone": "0.2.2", "key-tree-store": "~0.1.0",