diff --git a/src/FirebaseObject.js b/src/FirebaseObject.js index 85152e8b..8b0d3d86 100644 --- a/src/FirebaseObject.js +++ b/src/FirebaseObject.js @@ -188,17 +188,19 @@ * Called by $firebase whenever an item is changed at the server. * This method must exist on any objectFactory passed into $firebase. * - * @param snap + * It should return true if any changes were made, otherwise `$$notify` will + * not be invoked. + * + * @param {object} snap a Firebase snapshot + * @return {boolean} true if any changes were made. */ $$updated: function (snap) { // applies new data to this object var changed = $firebaseUtils.updateRec(this, snap); + // applies any defaults set using $$defaults $firebaseUtils.applyDefaults(this, this.$$defaults); - if( changed ) { - // notifies $watch listeners and - // updates $scope if bound to a variable - this.$$notify(); - } + // returning true here causes $$notify to be triggered + return changed; }, /** @@ -225,8 +227,8 @@ }, /** - * Updates any bound scope variables and notifies listeners registered - * with $watch any time there is a change to data + * Updates any bound scope variables and + * notifies listeners registered with $watch */ $$notify: function() { var self = this, list = this.$$conf.listeners.slice(); diff --git a/src/firebase.js b/src/firebase.js index 7a66d129..c04b2160 100644 --- a/src/firebase.js +++ b/src/firebase.js @@ -261,7 +261,14 @@ var obj = new ObjectFactory($inst, destroy, def.promise); var ref = $inst.$ref(); var batch = $firebaseUtils.batch(); - var applyUpdate = batch(obj.$$updated, obj); + var applyUpdate = batch(function(snap) { + var changed = obj.$$updated(snap); + if( changed ) { + // notifies $watch listeners and + // updates $scope if bound to a variable + obj.$$notify(); + } + }); var error = batch(obj.$$error, obj); var resolve = batch(_resolveFn); diff --git a/tests/unit/FirebaseObject.spec.js b/tests/unit/FirebaseObject.spec.js index 65827952..b9ec0be9 100644 --- a/tests/unit/FirebaseObject.spec.js +++ b/tests/unit/FirebaseObject.spec.js @@ -248,6 +248,7 @@ describe('$FirebaseObject', function() { $timeout.flush(); $fb.$set.calls.reset(); obj.$$updated(fakeSnap({foo: 'bar'})); + obj.$$notify(); flushAll(); expect($scope.test).toEqual({foo: 'bar', $id: obj.$id, $priority: obj.$priority}); });