Meteor defines ReactiveVar to access .events and .helpers

I am trying to define a new ReactiveVar variable to access in all template sections (e.g. .events, .helpers, .rendered ... etc) as shown in my code below, but I always get the error

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

      

Can someone please tell me what I am not seeing / doing wrong here? Thanks to

code:

  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);
        });
     }  
  });

      

+3


source to share


1 answer


You need to define ReactiveVar

in your template instance like this:

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

      

Then you will be able to access it in helpers like:



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

      

In events, you can use an argument 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);
    });
  }  
});

      

+6


source







All Articles