我尝试添加如下所示的可安装挂钩sails-hook-myoverride
:
let carryOn = (values, proceed, previous) => _.isFunction(previous) ? previous(values, proceed) : proceed();
module.exports = function (sails) {
return {
initialize: function (cb) {
sails.after(['hook:orm:loaded'], function () {
_.each(sails.models, (model) => {
let previousBeforeUpdate = model.beforeUpdate;
model.beforeUpdate = function (values, proceed) {
console.log("myoverride::beforeUpdate", values);
return carryOn(values, proceed, previousBeforeUpdate);
};
let previousBeforeCreate = model.beforeCreate;
model.beforeCreate = function (values, proceed) {
console.log("myoverride::beforeCreate", values);
return carryOn(values, proceed, previousBeforeCreate);
};
});
cb();
});
}
}
};
然后在我的测试模型中,我同时添加了beforeUpdate和beforeCreate方法:
beforeUpdate: function (values, cb) {
console.log('Test::beforeUpdate');
cb();
},
beforeCreate: function (values, cb) {
console.log('Test::beforeCreate');
cb();
}
然后我在http://localhost:1337/test上致电POST进行测试。
出乎意料的是,风帆只打印了Test::beforeCreate
,而没有打印myoverride::beforeCreate
,就像我希望的那样。
Google没有帮助,Sails文档也没有帮助:( 此刻我完全迷路了...
答案 0 :(得分:0)
好的,显然using sails.after(['hook:sockets:loaded'], function () {
出于某种原因起作用了……