Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add removePost function to be like removePre #21

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
14 changes: 14 additions & 0 deletions hooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,20 @@ module.exports = {
}
return this;
},
removePost: function (name, fnToRemove) {
var proto = this.prototype || this
, posts = proto._posts || (proto._posts || {});
if (!posts[name]) return this;
if (arguments.length === 1) {
// Remove all post callbacks for hook `name`
posts[name].length = 0;
} else {
posts[name] = posts[name].filter( function (currFn) {
return currFn !== fnToRemove;
});
}
return this;
},
_lazySetupHooks: function (proto, methodName, errorCb) {
if ('undefined' === typeof proto[methodName].numAsyncPres) {
this.hook(methodName, proto[methodName], errorCb);
Expand Down
46 changes: 46 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -626,6 +626,52 @@ module.exports = {
should.strictEqual(undefined, a.preValueOne);
should.strictEqual(undefined, a.preValueTwo);
},

'should be able to remove a particular post': function () {
var A = function () {}
, postTwo;
_.extend(A, hooks);
A.hook('save', function () {
this.value = 1;
});
A.post('save', function (next) {
this.postValueOne = 2;
next();
});
A.post('save', postTwo = function (next) {
this.postValueTwo = 4;
next();
});
A.removePost('save', postTwo);
var a = new A();
a.save();
a.value.should.equal(1);
a.postValueOne.should.equal(2);
should.strictEqual(undefined, a.postValueTwo);
},

'should be able to remove all posts associated with a hook': function () {
var A = function () {};
_.extend(A, hooks);
A.hook('save', function () {
this.value = 1;
});
A.post('save', function (next) {
this.postValueOne = 2;
next();
});
A.post('save', function (next) {
this.postValueTwo = 4;
next();
});
A.removePost('save');
var a = new A();
a.save();
a.value.should.equal(1);
should.strictEqual(undefined, a.postValueOne);
should.strictEqual(undefined, a.postValueTwo);
},


'#pre should lazily make a method hookable': function () {
var A = function () {};
Expand Down