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

Commit 021ed45

Browse files
author
Jacob Wenger
committed
Merge pull request #389 from firebase/release_0.8.1_idfix
Fixes #387 & #376 - set id on init instead of each update
2 parents 594b834 + 8ef4dd2 commit 021ed45

File tree

6 files changed

+40
-17
lines changed

6 files changed

+40
-17
lines changed

dist/angularfire.js

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -134,10 +134,15 @@
134134
*/
135135
$save: function(indexOrItem) {
136136
this._assertNotDestroyed('$save');
137-
var item = this._resolveItem(indexOrItem);
138-
var key = this.$keyAt(item);
137+
var self = this;
138+
var item = self._resolveItem(indexOrItem);
139+
var key = self.$keyAt(item);
139140
if( key !== null ) {
140-
return this.$inst().$set(key, $firebaseUtils.toJSON(item));
141+
return self.$inst().$set(key, $firebaseUtils.toJSON(item))
142+
.then(function(ref) {
143+
self._notify('child_changed', key);
144+
return ref;
145+
});
141146
}
142147
else {
143148
return $firebaseUtils.reject('Invalid record; could determine its key: '+indexOrItem);
@@ -600,12 +605,12 @@
600605
}
601606
// be sure to do this after setting up data and init state
602607
angular.forEach(self.$$conf.listeners, function (parts) {
603-
parts[0].call(parts[1], {event: 'updated', key: self.$id});
608+
parts[0].call(parts[1], {event: 'value', key: self.$id});
604609
});
605610
}
606611
};
607612

608-
self.$id = $firebase.$ref().name();
613+
self.$id = $firebase.$ref().ref().name();
609614
self.$priority = null;
610615
}
611616

@@ -615,7 +620,12 @@
615620
* @returns a promise which will resolve after the save is completed.
616621
*/
617622
$save: function () {
618-
return this.$inst().$set($firebaseUtils.toJSON(this));
623+
var notify = this.$$conf.notify;
624+
return this.$inst().$set($firebaseUtils.toJSON(this))
625+
.then(function(ref) {
626+
notify();
627+
return ref;
628+
});
619629
},
620630

621631
/**
@@ -758,7 +768,6 @@
758768
$$updated: function (snap) {
759769
// applies new data to this object
760770
var changed = $firebaseUtils.updateRec(this, snap);
761-
this.$id = snap.name();
762771
if( changed ) {
763772
// notifies $watch listeners and
764773
// updates $scope if bound to a variable

dist/angularfire.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/FirebaseArray.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@
110110
.then(function(ref) {
111111
self._notify('child_changed', key);
112112
return ref;
113-
})
113+
});
114114
}
115115
else {
116116
return $firebaseUtils.reject('Invalid record; could determine its key: '+indexOrItem);

src/FirebaseObject.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@
6464
}
6565
};
6666

67-
self.$id = $firebase.$ref().name();
67+
self.$id = $firebase.$ref().ref().name();
6868
self.$priority = null;
6969
}
7070

@@ -222,7 +222,6 @@
222222
$$updated: function (snap) {
223223
// applies new data to this object
224224
var changed = $firebaseUtils.updateRec(this, snap);
225-
this.$id = snap.name();
226225
if( changed ) {
227226
// notifies $watch listeners and
228227
// updates $scope if bound to a variable

tests/unit/FirebaseArray.spec.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -671,6 +671,7 @@ describe('$FirebaseArray', function () {
671671
}
672672
})();
673673

674+
//todo abstract into testutils
674675
function stubFb() {
675676
var ref = testutils.ref();
676677
var fb = {};

tests/unit/FirebaseObject.spec.js

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,19 @@ describe('$FirebaseObject', function() {
2828
});
2929
});
3030

31+
describe('constructor', function() {
32+
it('should set the record id', function() {
33+
expect(obj.$id).toEqual($fb.$ref().name());
34+
});
35+
36+
it('should accept a query', function() {
37+
var obj = makeObject(FIXTURE_DATA, $fb.$ref().limit(1).startAt(null));
38+
obj.$$updated(testutils.snap({foo: 'bar'}));
39+
flushAll();
40+
expect(obj).toEqual(jasmine.objectContaining({foo: 'bar'}));
41+
});
42+
});
43+
3144
describe('$save', function () {
3245
it('should call $firebase.$set', function () {
3346
obj.foo = 'bar';
@@ -417,9 +430,9 @@ describe('$FirebaseObject', function() {
417430
});
418431

419432
it('should preserve the id', function() {
420-
var oldId = obj.$id;
433+
obj.$id = 'change_id_for_test';
421434
obj.$$updated(fakeSnap(true));
422-
expect(obj.$id).toBe(oldId);
435+
expect(obj.$id).toBe('change_id_for_test');
423436
});
424437

425438
it('should set the priority', function() {
@@ -463,8 +476,9 @@ describe('$FirebaseObject', function() {
463476
return testutils.refSnap(testutils.ref('data/a'), data, pri);
464477
}
465478

466-
function stubFb() {
467-
var ref = testutils.ref('data/a');
479+
//todo abstract into testutils
480+
function stubFb(ref) {
481+
if( !ref ) { ref = testutils.ref('data/a'); }
468482
var fb = {};
469483
[
470484
'$set', '$update', '$remove', '$transaction', '$asArray', '$asObject', '$ref', '$push'
@@ -497,10 +511,10 @@ describe('$FirebaseObject', function() {
497511

498512
function noop() {}
499513

500-
function makeObject(initialData) {
514+
function makeObject(initialData, ref) {
501515
var readyFuture = $utils.defer();
502516
var destroyFn = jasmine.createSpy('destroyFn');
503-
var fb = stubFb();
517+
var fb = stubFb(ref);
504518
var obj = new $FirebaseObject(fb, destroyFn, readyFuture.promise);
505519
obj.$$$readyFuture = readyFuture;
506520
obj.$$$destroyFn = destroyFn;

0 commit comments

Comments
 (0)