Skip to content

Commit

Permalink
add constructor "destroy" event
Browse files Browse the repository at this point in the history
  • Loading branch information
tj committed Nov 4, 2012
1 parent d945526 commit c389b5b
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
4 changes: 3 additions & 1 deletion lib/proto.js
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ exports.validate = function(){
* Events:
*
* - `destroy` on deletion
* - `destroy` (model) on the constructor
*
* @param {Function} [fn]
* @api public
Expand All @@ -132,8 +133,9 @@ exports.destroy = function(fn){
this.emit('destroying');
request.del(url, function(res){
if (res.error) return fn(error(res));
self.emit('destroy');
self.destroyed = true;
self.emit('destroy');
self.model.emit('destroy', self);
fn();
});
};
Expand Down
12 changes: 12 additions & 0 deletions test/model.js
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,18 @@ describe('Model#destroy()', function(){
pet.destroy();
});
})

it('should emit "destroy" on the constructor', function(done){
var pet = new Pet({ name: 'Tobi' });
pet.save(function(err){
assert(!err);
Pet.once('destroy', function(obj){
assert(pet == obj);
done();
});
pet.destroy();
});
})
})
})

Expand Down

0 comments on commit c389b5b

Please sign in to comment.