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

Commit b5fa78c

Browse files
author
Jacob Wenger
committed
Merge pull request #456 from firebase/kato-remove_method
Added $remove method to $FirebaseObject
2 parents 55fa523 + cceb58c commit b5fa78c

File tree

2 files changed

+54
-0
lines changed

2 files changed

+54
-0
lines changed

src/FirebaseObject.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,22 @@
7575
});
7676
},
7777

78+
/**
79+
* Removes all keys from the FirebaseObject and also removes
80+
* the remote data from the server.
81+
*
82+
* @returns a promise which will resolve after the op completes
83+
*/
84+
$remove: function() {
85+
var self = this;
86+
$firebaseUtils.trimKeys(this, {});
87+
this.$value = null;
88+
return self.$inst().$remove(self.$id).then(function(ref) {
89+
self.$$notify();
90+
return ref;
91+
});
92+
},
93+
7894
/**
7995
* The loaded method is invoked after the initial batch of data arrives from the server.
8096
* When this resolves, all data which existed prior to calling $asObject() is now cached

tests/unit/FirebaseObject.spec.js

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -346,6 +346,44 @@ describe('$FirebaseObject', function() {
346346
});
347347
});
348348

349+
describe('$remove', function() {
350+
it('should return a promise', function() {
351+
expect(obj.$remove()).toBeAPromise();
352+
});
353+
354+
it('should set $value to null and remove any local keys', function() {
355+
expect($utils.dataKeys(obj)).toEqual($utils.dataKeys(FIXTURE_DATA));
356+
obj.$remove();
357+
flushAll();
358+
expect($utils.dataKeys(obj)).toEqual([]);
359+
});
360+
361+
it('should call $remove on the Firebase ref', function() {
362+
expect(obj.$inst().$remove).not.toHaveBeenCalled();
363+
obj.$remove();
364+
flushAll();
365+
expect(obj.$inst().$remove).toHaveBeenCalled();
366+
});
367+
368+
it('should delete a primitive value', function() {
369+
var snap = fakeSnap('foo');
370+
obj.$$updated(snap);
371+
flushAll();
372+
expect(obj.$value).toBe('foo');
373+
obj.$remove();
374+
flushAll();
375+
expect(obj.$value).toBe(null);
376+
});
377+
378+
it('should trigger a value event for $watch listeners', function() {
379+
var spy = jasmine.createSpy('$watch listener');
380+
obj.$watch(spy);
381+
obj.$remove();
382+
flushAll();
383+
expect(spy).toHaveBeenCalledWith({ event: 'value', key: obj.$id });
384+
});
385+
});
386+
349387
describe('$destroy', function () {
350388
it('should invoke destroyFn', function () {
351389
obj.$destroy();

0 commit comments

Comments
 (0)