Skip to content
This repository was archived by the owner on Mar 17, 2025. It is now read-only.

Commit c22b5ad

Browse files
author
Jacob Wenger
committed
Merge pull request #457 from firebase/kato-extendFactory
Simplified $$updated method in $FirebaseObject
2 parents 9fdffec + b587e18 commit c22b5ad

File tree

3 files changed

+19
-9
lines changed

3 files changed

+19
-9
lines changed

src/FirebaseObject.js

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -188,17 +188,19 @@
188188
* Called by $firebase whenever an item is changed at the server.
189189
* This method must exist on any objectFactory passed into $firebase.
190190
*
191-
* @param snap
191+
* It should return true if any changes were made, otherwise `$$notify` will
192+
* not be invoked.
193+
*
194+
* @param {object} snap a Firebase snapshot
195+
* @return {boolean} true if any changes were made.
192196
*/
193197
$$updated: function (snap) {
194198
// applies new data to this object
195199
var changed = $firebaseUtils.updateRec(this, snap);
200+
// applies any defaults set using $$defaults
196201
$firebaseUtils.applyDefaults(this, this.$$defaults);
197-
if( changed ) {
198-
// notifies $watch listeners and
199-
// updates $scope if bound to a variable
200-
this.$$notify();
201-
}
202+
// returning true here causes $$notify to be triggered
203+
return changed;
202204
},
203205

204206
/**
@@ -225,8 +227,8 @@
225227
},
226228

227229
/**
228-
* Updates any bound scope variables and notifies listeners registered
229-
* with $watch any time there is a change to data
230+
* Updates any bound scope variables and
231+
* notifies listeners registered with $watch
230232
*/
231233
$$notify: function() {
232234
var self = this, list = this.$$conf.listeners.slice();

src/firebase.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -261,7 +261,14 @@
261261
var obj = new ObjectFactory($inst, destroy, def.promise);
262262
var ref = $inst.$ref();
263263
var batch = $firebaseUtils.batch();
264-
var applyUpdate = batch(obj.$$updated, obj);
264+
var applyUpdate = batch(function(snap) {
265+
var changed = obj.$$updated(snap);
266+
if( changed ) {
267+
// notifies $watch listeners and
268+
// updates $scope if bound to a variable
269+
obj.$$notify();
270+
}
271+
});
265272
var error = batch(obj.$$error, obj);
266273
var resolve = batch(_resolveFn);
267274

tests/unit/FirebaseObject.spec.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,7 @@ describe('$FirebaseObject', function() {
248248
$timeout.flush();
249249
$fb.$set.calls.reset();
250250
obj.$$updated(fakeSnap({foo: 'bar'}));
251+
obj.$$notify();
251252
flushAll();
252253
expect($scope.test).toEqual({foo: 'bar', $id: obj.$id, $priority: obj.$priority});
253254
});

0 commit comments

Comments
 (0)