2014-12-22 41 views
3

我想要定義一個新的ReactiveVar變量,使其可以在所有模板部分(如.events,.helpers,.rendered等)中訪問,如圖所示在我的代碼下面,但我總是得到一個錯誤:流星定義ReactiveVar可在.events和.helpers中訪問

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

回答

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