有更好的方法来执行req.flash消息吗?就像将所有flash消息添加到一个对象的函数一样?
app.get('/forgot', function(req, res) {
var info = req.flash('info'),
error = req.flash('error'),
success = req.flash('success'),
user = req.user;
var send = {};
send.user = user;
send.messages = {
info: info,
error: error,
success: success
}
res.render('forgot', send);
});
答案 0 :(得分:0)
我做了自己的剧本。
helper.js
module.exports = function(req, res){
return function(){
var buf = '',
messages = req.flash(),
types = Object.keys(messages);
for(var i = 0; i < types.length; ++i){
var type = types[i],
msgs = messages[type];
for(var j = 0; j < msgs.length; ++j){
buf += '<div class="alert alert-' + type + ' alert-dismissable"><a class="panel-close close" data-dismiss="alert">×</a>' + msgs[j] + '</div>';
}
}
return buf;
}
};
server.js
app.use(function (req, res, next) {
res.locals.messages = require('./config/helpers.js')(req, res);
next();
});
index.jade
!= messages()