Meteor定义Reactive Var可在.events和.helpers中访问

时间:2014-12-22 22:57:30

标签: meteor

我正在尝试定义一个新的ReactiveVar变量,可以在所有模板部分中访问(例如.events,.helpers,.rendered ......等),如下面的代码所示,但我总是收到错误:

Error: Exception in template helper:
ReferenceError: logData is not defined

有人可以告诉我这里错过了什么/做错了吗?感谢

代码:

  Template.detailedreport.rendered = function() {

       var logData = new ReactiveVar;
       logData.set([]);
  };


  Template.detailedreport.helpers({
      myCollection: function () {
        return logData.get();
      }
  });

  Template.detailedreport.events({
     'submit form': function(e) {
        e.preventDefault();
        var now  = Session.get("startDate");
        var then = Session.get("endDate");
        var custID = Session.get("customer");
        var projID = Session.get("project");
        Meteor.call('logSummary', now, then, projID, custID, function(error, data){
          if(error)
            return alert(error.reason);

          logData.set(data);
        });
     }  
  });

1 个答案:

答案 0 :(得分:6)

您需要在模板实例上定义ReactiveVar,如下所示:

Template.detailedreport.created = function() {
  this.logData = new ReactiveVar([]);
};

然后你就可以在这样的助手中访问它了:

Template.detailedreport.helpers({
  myCollection: function () {
    return Template.instance().logData.get();
  }
});

在事件中,您可以使用template参数:

Template.detailedreport.events({
  'submit form': function(e, template) {
    e.preventDefault();
    var now  = Session.get("startDate");
    var then = Session.get("endDate");
    var custID = Session.get("customer");
    var projID = Session.get("project");
    Meteor.call('logSummary', now, then, projID, custID, function(error, data){
      if(error){
        return alert(error.reason);
      }
      template.logData.set(data);
    });
  }  
});