我希望能够将多个数据集传递给我的视图。这是我目前在控制器中的操作方式:
transactions: function (req, res) {
var queryexpenses = 'select * from expense order by name';
Expense.query(queryexpenses, function (err, expense) {
this.expenses = expense;
});
if (req.param('filter')) {
var where = 'where fk_expense = ' + req.param('expensefilter');
where += ' and datePosted > "' + req.param('yearfilter') + '-01-01" ';
where += ' and datePosted < "' + req.param('yearfilter') + '-12-31" ';
} else {
var where = 'where fk_expense IS NULL';
}
var query = 'select * from accounting ' + where + ' order by description';
Accounting.query(query, function (err, trans) {
this.transactions = trans;
});
var total = 0;
_.each(this.transactions, function (element, index, list) {
// format dates
element.datePosted = dateFormat(element.datePosted, 'dd/mm/yyyy');
var tmp0 = element.amount
var tmp1 = tmp0.replace(/ /g, '');
var tmp2 = parseFloat(tmp1);
total += tmp2;
});
this.total = total.toFixed(2);
return res.view();
}
这是我能够完成我试图做的事情的唯一方法,但我认为有些问题是由于我将查询对象放在“this”范围内造成的。第一个问题是第一次重新加载服务器重启后页面会崩溃。第二个问题是一切似乎都落后了一步。我的意思是,如果我在 UI 上发出命令(例如提交表单),除非我两次执行相同的操作,否则什么都不会发生。
那么如何将多组数据传递给我的视图而不将它们限定在“this”中?