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

Commit b8ac87a

Browse files
committed
Fixes #368 - trigger $watch event on $save
Modified $FirebaseObject's watch `event` to be "value"
1 parent 2df3edc commit b8ac87a

File tree

4 files changed

+34
-5
lines changed

4 files changed

+34
-5
lines changed

src/FirebaseArray.js

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -102,10 +102,15 @@
102102
*/
103103
$save: function(indexOrItem) {
104104
this._assertNotDestroyed('$save');
105-
var item = this._resolveItem(indexOrItem);
106-
var key = this.$keyAt(item);
105+
var self = this;
106+
var item = self._resolveItem(indexOrItem);
107+
var key = self.$keyAt(item);
107108
if( key !== null ) {
108-
return this.$inst().$set(key, $firebaseUtils.toJSON(item));
109+
return self.$inst().$set(key, $firebaseUtils.toJSON(item))
110+
.then(function(ref) {
111+
self._notify('child_changed', key);
112+
return ref;
113+
})
109114
}
110115
else {
111116
return $firebaseUtils.reject('Invalid record; could determine its key: '+indexOrItem);

src/FirebaseObject.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@
5959
}
6060
// be sure to do this after setting up data and init state
6161
angular.forEach(self.$$conf.listeners, function (parts) {
62-
parts[0].call(parts[1], {event: 'updated', key: self.$id});
62+
parts[0].call(parts[1], {event: 'value', key: self.$id});
6363
});
6464
}
6565
};
@@ -74,7 +74,12 @@
7474
* @returns a promise which will resolve after the save is completed.
7575
*/
7676
$save: function () {
77-
return this.$inst().$set($firebaseUtils.toJSON(this));
77+
var notify = this.$$conf.notify;
78+
return this.$inst().$set($firebaseUtils.toJSON(this))
79+
.then(function(ref) {
80+
notify();
81+
return ref;
82+
});
7883
},
7984

8085
/**

tests/unit/FirebaseArray.spec.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,16 @@ describe('$FirebaseArray', function () {
203203
arr.$save(0);
204204
}).toThrowError(Error);
205205
});
206+
207+
it('should trigger watch event', function() {
208+
var spy = jasmine.createSpy('$watch');
209+
arr.$watch(spy);
210+
var key = arr.$keyAt(1);
211+
arr[1].foo = 'watchtest';
212+
arr.$save(1);
213+
flushAll();
214+
expect(spy).toHaveBeenCalledWith(jasmine.objectContaining({event: 'child_changed', key: key}));
215+
});
206216
});
207217

208218
describe('$remove', function() {

tests/unit/FirebaseObject.spec.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,15 @@ describe('$FirebaseObject', function() {
5757
expect(whiteSpy).not.toHaveBeenCalled();
5858
expect(blackSpy).toHaveBeenCalledWith('test_fail');
5959
});
60+
61+
it('should trigger watch event', function() {
62+
var spy = jasmine.createSpy('$watch');
63+
obj.$watch(spy);
64+
obj.foo = 'watchtest';
65+
obj.$save();
66+
flushAll();
67+
expect(spy).toHaveBeenCalledWith(jasmine.objectContaining({event: 'value', key: obj.$id}));
68+
});
6069
});
6170

6271
describe('$loaded', function () {

0 commit comments

Comments
 (0)