Skip to content
This repository was archived by the owner on Mar 17, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 13 additions & 10 deletions src/firebase.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,33 +72,36 @@
},

$remove: function (key) {
var ref = this._ref, self = this, promise;
var ref = this._ref, self = this;
var def = $firebaseUtils.defer();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Declared and not used : ( Great find.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank Webstorm for that one.

if (arguments.length > 0) {
ref = ref.ref().child(key);
}
if( angular.isFunction(ref.remove) ) {
// self is not a query, just do a flat remove
ref.remove(self._handle(def, ref));
promise = def.promise;
}
else {
var promises = [];
// self is a query so let's only remove the
// items in the query and not the entire path
ref.once('value', function(snap) {
var promises = [];
snap.forEach(function(ss) {
var d = $firebaseUtils.defer();
promises.push(d);
ss.ref().remove(self._handle(d, ss.ref()));
promises.push(d.promise);
ss.ref().remove(self._handle(d));
}, self);
$firebaseUtils.allPromises(promises)
.then(function() {
def.resolve(ref);
},
function(err){
def.reject(err);
}
);
});
promise = $firebaseUtils.allPromises(promises)
.then(function() {
return ref;
});
}
return promise;
return def.promise;
},

$update: function (key, data) {
Expand Down
17 changes: 16 additions & 1 deletion tests/unit/firebase.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,8 @@ describe('$firebase', function () {
var ref = new Firebase('Mock://').child('ordered').limit(2);
var $fb = $firebase(ref);
$fb.$remove().then(spy);
flushAll();
flushAll(ref);
flushAll(ref);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You need to do a double call to flushAll() for this test to pass.
Seems like a bug somewhere. (Mockfirebase maybe?).

expect(spy).toHaveBeenCalledWith(ref);
});

Expand Down Expand Up @@ -305,6 +306,20 @@ describe('$firebase', function () {
}
});
});

it('should wait to resolve promise until data is actually deleted',function(){
var ref = new Firebase('Mock://').child('ordered').limit(2);
var $fb = $firebase(ref);
var resolved = false;
$fb.$remove().then(function(){
resolved = true;
});
try {$timeout.flush();} catch(e){} //this may actually throw an error
expect(resolved).toBe(false);
flushAll(ref);
flushAll(ref);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here as well.

expect(resolved).toBe(true);
});
});

describe('$update', function() {
Expand Down