diff --git a/firebase-database-behavior.html b/firebase-database-behavior.html index 4c7d05d..127b8cd 100644 --- a/firebase-database-behavior.html +++ b/firebase-database-behavior.html @@ -90,9 +90,10 @@ }, __pathChanged: function(path, oldPath) { - if (oldPath != null && !this.disabled && this.__pathReady(path)) { + if (!this.disabled && !this.valueIsEmpty(this.data)) { this.syncToMemory(function() { this.data = this.zeroValue; + this.__needSetData = true; }); } }, diff --git a/firebase-document.html b/firebase-document.html index 285258b..e43bf3d 100644 --- a/firebase-document.html +++ b/firebase-document.html @@ -39,6 +39,7 @@ ], attached: function() { + this.__needSetData = true; this.__refChanged(this.ref, this.ref); }, @@ -184,13 +185,30 @@ if (value == null) { value = this.zeroValue; + this.__needSetData = true; } - if (!this.new) { + if (!this.isNew) { this.async(function() { this.syncToMemory(function() { this._log('Updating data from Firebase value:', value); - this.set('data', value); + + // set the value if: + // it is the first time we run this (or the path has changed and we are back with zeroValue) + // or if this.data does not exist + // or value is primitive + // or if firebase value obj contain less keys than this.data (https://github.com/Polymer/polymer/issues/2565) + if (this.__needSetData || !this.data || typeof value !== 'object' || ( Object.keys(value).length < Object.keys(this.data).length)) { + this.__needSetData = false; + return this.set('data', value); + } + + // now, we loop over keys + for (var prop in value) { + if(value[prop] !== this.data[prop]) { + this.set(['data', prop], value[prop]); + } + } }); }); }