diff --git a/src/FirebaseObject.js b/src/FirebaseObject.js index c8155c15..85152e8b 100644 --- a/src/FirebaseObject.js +++ b/src/FirebaseObject.js @@ -75,6 +75,22 @@ }); }, + /** + * Removes all keys from the FirebaseObject and also removes + * the remote data from the server. + * + * @returns a promise which will resolve after the op completes + */ + $remove: function() { + var self = this; + $firebaseUtils.trimKeys(this, {}); + this.$value = null; + return self.$inst().$remove(self.$id).then(function(ref) { + self.$$notify(); + return ref; + }); + }, + /** * The loaded method is invoked after the initial batch of data arrives from the server. * When this resolves, all data which existed prior to calling $asObject() is now cached diff --git a/tests/unit/FirebaseObject.spec.js b/tests/unit/FirebaseObject.spec.js index 64ff95cd..65827952 100644 --- a/tests/unit/FirebaseObject.spec.js +++ b/tests/unit/FirebaseObject.spec.js @@ -346,6 +346,44 @@ describe('$FirebaseObject', function() { }); }); + describe('$remove', function() { + it('should return a promise', function() { + expect(obj.$remove()).toBeAPromise(); + }); + + it('should set $value to null and remove any local keys', function() { + expect($utils.dataKeys(obj)).toEqual($utils.dataKeys(FIXTURE_DATA)); + obj.$remove(); + flushAll(); + expect($utils.dataKeys(obj)).toEqual([]); + }); + + it('should call $remove on the Firebase ref', function() { + expect(obj.$inst().$remove).not.toHaveBeenCalled(); + obj.$remove(); + flushAll(); + expect(obj.$inst().$remove).toHaveBeenCalled(); + }); + + it('should delete a primitive value', function() { + var snap = fakeSnap('foo'); + obj.$$updated(snap); + flushAll(); + expect(obj.$value).toBe('foo'); + obj.$remove(); + flushAll(); + expect(obj.$value).toBe(null); + }); + + it('should trigger a value event for $watch listeners', function() { + var spy = jasmine.createSpy('$watch listener'); + obj.$watch(spy); + obj.$remove(); + flushAll(); + expect(spy).toHaveBeenCalledWith({ event: 'value', key: obj.$id }); + }); + }); + describe('$destroy', function () { it('should invoke destroyFn', function () { obj.$destroy();