【发布时间】:2015-06-21 21:35:33
【问题描述】:
有没有办法在 Sails 控制器中定义的每个和所有操作之前执行一个操作/功能?类似于模型中的beforeCreate 钩子。
例如在我的 DataController 中,我有以下操作:
module.exports = {
mockdata: function(req, res) {
var criteria = {};
// collect all params
criteria = _.merge({}, req.params.all(), req.body);
//...some more login with the criteria...
},
getDataForHost: function(req, res) {
var criteria = {};
// collect all params
criteria = _.merge({}, req.params.all(), req.body);
//...some more login with the criteria...
}
};
我可以执行以下操作吗:
module.exports = {
beforeAction: function(req, res, next) {
var criteria = {};
// collect all params
criteria = _.merge({}, req.params.all(), req.body);
// store the criteria somewhere for later use
// or perhaps pass them on to the next call
next();
},
mockdata: function(req, res) {
//...some more login with the criteria...
},
getDataForHost: function(req, res) {
//...some more login with the criteria...
}
};
对定义的任何操作的任何调用将首先通过 beforeAction?
【问题讨论】:
-
改用策略,它会让你的代码看起来更清晰