如何在sailsjs中将多个数据集传递给我的视图?

时间:2013-11-05 02:37:19

标签: node.js orm express sails.js

我希望能够将多个数据集传递给我的视图。以下是我目前在我的控制器中执行此操作的方法:

  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”中进行限定?

1 个答案:

答案 0 :(得分:0)

res.view({
    corndogs: [{name: 'Hank the Corndog'}, {name: 'Lenny the Corndog'}]
});

以下是相关文档页面:http://sailsjs.org/#!documentation/views

此外,您似乎没有充分利用Waterline进行SQL查询。