在Sails控制器中定義的每個和所有操作之前是否有一種方法可以執行操作/函數?類似於模型中的beforeCreate
掛鉤。在Sails控制器中執行操作之前
舉例來說,在我的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第一?
使用政策相反,它會使你的代碼看起來更清晰 –