Permalink
Browse files

Now, lazy hook creation via #pre can pass an errorHandler along to th…

…e hook creation method #hook
  • Loading branch information...
1 parent c145d60 commit 1f90ff16ac0940e880fd89b6f3ce965c51656e73 @bnoguchi committed Jun 14, 2011
Showing with 24 additions and 5 deletions.
  1. +6 −5 hooks.js
  2. +18 −0 test.js
View
@@ -101,15 +101,16 @@ module.exports = {
return this;
},
- pre: function (name, isAsync, fn) {
- if (arguments.length === 2) {
+ pre: function (name, isAsync, fn, errorCb) {
+ if ('boolean' !== typeof arguments[1]) {
+ errorCb = fn;
fn = isAsync;
isAsync = false;
}
var proto = this.prototype || this
, pres = proto._pres = proto._pres || {};
- this._lazySetupHooks(proto, name);
+ this._lazySetupHooks(proto, name, errorCb);
if (fn.isAsync = isAsync) {
proto[name].numAsyncPres++;
@@ -144,9 +145,9 @@ module.exports = {
}
return this;
},
- _lazySetupHooks: function (proto, methodName) {
+ _lazySetupHooks: function (proto, methodName, errorCb) {
if ('undefined' === typeof proto[methodName].numAsyncPres) {
- this.hook(methodName, proto[methodName]);
+ this.hook(methodName, proto[methodName], errorCb);
}
}
};
View
18 test.js
@@ -597,6 +597,24 @@ module.exports = {
a.preValue.should.equal(2);
},
+ '#pre lazily making a method hookable should be able to provide an errorHandler as the last argument': function () {
+ var A = function () {};
+ var preValue = "";
+ _.extend(A, hooks);
+ A.prototype.save = function () {
+ this.value = 1;
+ };
+ A.pre('save', function (next) {
+ next(new Error);
+ }, function (err) {
+ preValue = 'ERROR';
+ });
+ var a = new A();
+ a.save();
+ should.strictEqual(undefined, a.value);
+ preValue.should.equal('ERROR');
+ },
+
'#post should lazily make a method hookable': function () {
var A = function () {};
_.extend(A, hooks);

0 comments on commit 1f90ff1

Please sign in to comment.