From 5817877aa100f4aecb6a8f0c021d20423e34a668 Mon Sep 17 00:00:00 2001 From: Matthew Creager Date: Mon, 28 Apr 2014 14:08:20 -0300 Subject: [PATCH] fix(key_sync): optimize model updates Removing dead properties and adding new properties now occurs within a single `$timeout`. Closes: #95 Signed-off-by: Colin MacDonald --- lib/key_sync.js | 23 ++++++++++++----------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/lib/key_sync.js b/lib/key_sync.js index 508749b..8319dc2 100644 --- a/lib/key_sync.js +++ b/lib/key_sync.js @@ -103,16 +103,6 @@ KeySync.prototype.$$handleUpdate = function(value, context) { value = normalize(_.cloneDeep(value), context); - if (context.command === 'SET' && targetKey === context.currentKey) { - self.$timeout(function() { - _.each(self.$$model, function(value, key, model) { - if (_.first(key) !== '$') { - delete model[key]; - } - }); - }); - } - if (context.command === 'SET' && targetKey !== context.currentKey) { // Determine the relative path between the key we are listening too and the // origin of the event. @@ -151,8 +141,19 @@ KeySync.prototype.$$handleUpdate = function(value, context) { } self.$timeout(function() { + if (context.command === 'SET' && targetKey === context.currentKey) { + _.each(self.$$model, function(value, key, model) { + if (_.first(key) !== '$') { + delete model[key]; + } + }); + } + _.merge(self.$$model, value); - delete self.$$model.$value; + + if (self.$$model.$value) { + delete self.$$model.$value; + } }); };