Skip to content

Commit

Permalink
Merge cceb58c into 55fa523
Browse files Browse the repository at this point in the history
  • Loading branch information
katowulf committed Nov 13, 2014
2 parents 55fa523 + cceb58c commit e4f27dd
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 0 deletions.
16 changes: 16 additions & 0 deletions src/FirebaseObject.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
38 changes: 38 additions & 0 deletions tests/unit/FirebaseObject.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down

0 comments on commit e4f27dd

Please sign in to comment.