次の例では、helpers
オブジェクトの再利用を処理する最良の方法は何でしょうか?
var test = {
Projects: 'an object goes here',
helpers: require('../helpers'),
test: function(req, res) {
if (this.helpers.is_post(req)) {
// tried this
var test = this.helpers;
this.Projects.byCode(req.params.project_code, function(project) {
if (!project) {
this.helpers.flash(req, 'error', 'Unable to find project.');
// tried this
helpers.flash(req, 'error', 'Unable to find project.');
res.redirect('/projects');
}
});
}
}
};
同じランタイム中に実行されないため、コールバックで変数やオブジェクトなどを再利用できないことはわかっていますが、それでもそのようなことを行うためのより良い/より明確な方法が必要ですか?
this.helpers を別の変数に再割り当てしようとしても、未定義であるというエラーが表示されます。
ありがとう!