Meteor.js调用template.helpers函数vs全局变量

时间:2014-12-21 19:18:57

标签: meteor

我正在使用Reactive-table在我的meteor.js应用程序中显示分页数据,如下所示,但Reactive-table中显示的数据取决于特定的用户事件(选择客户端,项目,日期范围并单击提交按钮)。所以我想知道是否有可能触发template.helpers>>来自'提交表单'事件的myCollection函数?或者,最好定义一个全局变量来存储基于用户(客户端,项目,日期范围选择)从用户查询返回的数据,然后使这个全局变量从myCollection函数返回?

我曾尝试研究如何从template.events事件中调用.helpers函数,但找不到任何信息。因此,对于哪种方法更好的任何帮助,如果调用.events函数更好,那么如何做到这一点,将受到高度赞赏。感谢。

以下是我的应用中的代码:

Template.detailedreport.rendered = function() {
     Session.set("dreport_customer", "");
     Session.set("dreport_project", "");
     Session.set("dreport_startDate", new Date());
     Session.set("dreport_endDate", new Date());

   $('.set-start-date').datetimepicker({
        pickTime: false,
        defaultDate: new Date()
   });
   $('.set-end-date').datetimepicker({
        pickTime: false,
        defaultDate: new Date()
   });  

  $('.set-start-date').on("dp.change",function (e) {
       Session.set("dreport_startDate", $('.set-start-date').data('DateTimePicker').getDate().toLocaleString());
    });
    $('.set-end-date').on("dp.change",function (e) {
        Session.set("dreport_endDate", $('.set-end-date').data('DateTimePicker').getDate().toLocaleString());
    });
};

Template.detailedreport.helpers({
    customerslist: function() {
       return Customers.find({}, {sort:{name: -1}});       
    },
    projectslist: function() { 
       return Projects.find({customerid: Session.get("dreport_customer")}, {sort:{title: -1}});       
    },
    myCollection: function () {
      var now  = Session.get("dreport_startDate");
      var then = Session.get("dreport_endDate");
      var custID = Session.get("dreport_customer");
      var projID = Session.get("dreport_project");
          Meteor.call('logSummary', now, then, projID, custID, function(error, data){
            if(error)
              return alert(error.reason);
            return data;
          });        
      }
    },      
    settings: function () {
        return {
            rowsPerPage: 10,
            showFilter: true,
            showColumnToggles: false,
            fields: [
                { key: '0._id.day', label: 'Day' },
                { key: '0.totalhours', label: 'Hours Spent'}                           
            ]
        };
    }

});

Template.detailedreport.events({
   'submit form': function(e) {
      e.preventDefault();
      var now  = $('.set-start-date').data('DateTimePicker').getDate().toLocaleString();
      var then = $('.set-end-date').data('DateTimePicker').getDate().toLocaleString();
      var custID = $(e.target).find('[name=customer]').val();
      var projID = $(e.target).find('[name=project]').val();
      //Here is the problem as I am not sure how to refresh myCollection function in .helpers
   },  
   'change #customer': function(e){
        Session.set("dreport_project", "");
    Session.set("dreport_customer", e.currentTarget.value);
   },
   'change #project': function(e){
    Session.set("dreport_project", e.currentTarget.value);
   }  
});



Template:

    <div>
      {{> reactiveTable class="table table-bordered table-hover" collection=myCollection settings=settings}}
    </div>




Server:

Meteor.methods({
  logSummary: function(startDate, endDate, projid, custid){
    //Left without filtering based on date, proj, cust for testing only...
  return Storylog.find({});
  }
});

1 个答案:

答案 0 :(得分:1)

模板助手是反应性的,这意味着如果它们的依赖关系发生变化,它们将被重新计算。所以你需要做的就是更新它们的依赖关系,然后重新计算myCollection帮助器。

将评论// Here is the problem...替换为:

Session.set('dreport_endDate', then);
Session.set('dreport_startDate', now);
Session.set('dreport_project', projID);
Session.set('dreport_customer', custID);
相关问题