2013-04-11 23:23:34 +00:00
|
|
|
/**
|
|
|
|
* Module exports
|
|
|
|
*/
|
2013-05-28 05:20:30 +00:00
|
|
|
module.exports = Hookable;
|
2011-11-17 04:24:43 +00:00
|
|
|
|
2013-04-11 23:23:34 +00:00
|
|
|
/**
|
2013-05-28 05:20:30 +00:00
|
|
|
* Hooks mixins
|
2013-04-11 23:23:34 +00:00
|
|
|
*/
|
2013-05-28 05:20:30 +00:00
|
|
|
|
|
|
|
function Hookable() {
|
|
|
|
}
|
2011-11-17 04:24:43 +00:00
|
|
|
|
2013-04-11 23:23:34 +00:00
|
|
|
/**
|
|
|
|
* List of hooks available
|
|
|
|
*/
|
2011-11-20 05:36:15 +00:00
|
|
|
Hookable.afterInitialize = null;
|
2013-03-28 11:23:31 +00:00
|
|
|
Hookable.beforeValidate = null;
|
|
|
|
Hookable.afterValidate = null;
|
2011-11-20 05:36:15 +00:00
|
|
|
Hookable.beforeSave = null;
|
|
|
|
Hookable.afterSave = null;
|
|
|
|
Hookable.beforeCreate = null;
|
|
|
|
Hookable.afterCreate = null;
|
|
|
|
Hookable.beforeUpdate = null;
|
|
|
|
Hookable.afterUpdate = null;
|
|
|
|
Hookable.beforeDestroy = null;
|
|
|
|
Hookable.afterDestroy = null;
|
2011-11-17 04:24:43 +00:00
|
|
|
|
2013-05-17 15:49:57 +00:00
|
|
|
// TODO: Evaluate https://github.com/bnoguchi/hooks-js/
|
2012-04-09 16:24:35 +00:00
|
|
|
Hookable.prototype.trigger = function trigger(actionName, work, data) {
|
2014-01-24 17:09:53 +00:00
|
|
|
var capitalizedName = capitalize(actionName);
|
|
|
|
var beforeHook = this.constructor["before" + capitalizedName]
|
|
|
|
|| this.constructor["pre" + capitalizedName];
|
|
|
|
var afterHook = this.constructor["after" + capitalizedName]
|
|
|
|
|| this.constructor["post" + capitalizedName];
|
|
|
|
if (actionName === 'validate') {
|
|
|
|
beforeHook = beforeHook || this.constructor.beforeValidation;
|
|
|
|
afterHook = afterHook || this.constructor.afterValidation;
|
|
|
|
}
|
|
|
|
var inst = this;
|
2011-11-20 05:36:15 +00:00
|
|
|
|
2014-01-24 17:09:53 +00:00
|
|
|
// we only call "before" hook when we have actual action (work) to perform
|
|
|
|
if (work) {
|
|
|
|
if (beforeHook) {
|
|
|
|
// before hook should be called on instance with one param: callback
|
|
|
|
beforeHook.call(inst, function () {
|
|
|
|
// actual action also have one param: callback
|
|
|
|
work.call(inst, next);
|
|
|
|
}, data);
|
2011-11-20 05:36:15 +00:00
|
|
|
} else {
|
2014-01-24 17:09:53 +00:00
|
|
|
work.call(inst, next);
|
2011-11-20 05:36:15 +00:00
|
|
|
}
|
2014-01-24 17:09:53 +00:00
|
|
|
} else {
|
|
|
|
next();
|
|
|
|
}
|
2011-11-17 04:24:43 +00:00
|
|
|
|
2014-01-24 17:09:53 +00:00
|
|
|
function next(done) {
|
|
|
|
if (afterHook) {
|
|
|
|
afterHook.call(inst, done);
|
|
|
|
} else if (done) {
|
|
|
|
done.call(this);
|
2011-11-17 07:00:12 +00:00
|
|
|
}
|
2014-01-24 17:09:53 +00:00
|
|
|
}
|
2011-11-17 07:00:12 +00:00
|
|
|
};
|
2011-11-17 04:24:43 +00:00
|
|
|
|
2011-11-17 07:00:12 +00:00
|
|
|
function capitalize(string) {
|
2014-01-24 17:09:53 +00:00
|
|
|
return string.charAt(0).toUpperCase() + string.slice(1);
|
2011-11-17 07:00:12 +00:00
|
|
|
}
|