如何在模板中过滤Meteor集合

时间:2015-02-14 00:39:39

标签: meteor filter routes

我有一个模板,我正在填充所有转移

Template.listTransfers.helpers({
  transfers: function () {
    var thisId = Meteor.userId();
    return Transfers.find({userId: thisId}, {sort: {timeCreated: -1}});
  },
});

我还在模板中有一个点击事件,需要过滤掉有发票的转移return Transfers.find({userId: thisId, 'invoice' : {$exists : 1}}, {sort: {timeCreated: -1}});

我将模板拉入传输中

 {{#each transfers}}
   {{> transferItem}}
 {{/each}}

有没有办法在该声明中执行此操作,还是需要创建单独的路径?

1 个答案:

答案 0 :(得分:1)

您可以在模板实例上使用ReactiveVar,如下所示:

Template.listTransfers.created({
  // Initialize a reactive variable on the template instance
  this.showInvoices = new ReactiveVar(true);
});

Template.listTransfers.helpers({
  transfers: function () {
    var thisId = Meteor.userId();
    var showInvoices = Template.instance().showInvoices;

    if (showInvoices.get()) {
      return Transfers.find({userId: thisId}, {sort: {timeCreated: -1}});
    } else {
      return Transfers.find({userId: thisId, 'invoice' : {$exists : 1}}, {sort: {timeCreated: -1}});
    }
  },
});

Template.listTransfers.events({
  "click button.hide-invoices": function () {
    var showInvoices = Template.instance().showInvoices;

    // Toggle the showInvoices var
    showInvoices.set(! showInvoices.get());
  }
})