【问题标题】:Reactive Template re-render meteor js after search搜索后反应模板重新渲染流星js
【发布时间】:2015-12-17 17:02:35
【问题描述】:

我通过发布和订阅编写了一个简单的搜索,我可以使用 console.log 看到搜索结果在服务器和客户端都正确显示,但是我的模板似乎没有重新呈现自己,尽管有跟踪器依赖。下面是客户端、发布和模板 onRendered 调用的代码。

客户代码

Template.list_customers.onRendered(function(event){
  console.log('Inside Rendered');
  Template.list_customers.__helpers[" getMyCustomers"]();
  //eventsUI.changed();
  eventsUI.changed();
});

Template.list_customers.helpers({
  getMyCustomers: function(searchTerm) {
    console.log('Search Term is ', searchTerm);

    // //Get the current user and its BP Id
      Meteor.subscribe("getUser", Meteor.userId());
      currentUser = Meteor.users.find({
        _id: Meteor.userId()
      }).fetch();

      currentUserBPId = currentUser[0].profile.BusinessPartnerId;
      //Get all the BP's which the logged in BP sells to

      Meteor.subscribe("getCustomerRelations", currentUserBPId);
      customer_cursor = BusinessPartnerRelations.find({
        "bp_subject": currentUserBPId,
        "relation": "sells_to"
      }).fetch();

      bp_predicates = customer_cursor.map(function(c) {
        return c.bp_predicate[0]
      });

      Deps.autorun(function() {
        handlePagination = Meteor.subscribeWithPagination("getCustomers", bp_predicates, 25,searchTerm);
      });

        if(searchTerm){
          console.log(searchTerm);
          customers = BusinessPartners.find({
            score:{"$exists":true}
          }).fetch();

        }
        else {
          customers = BusinessPartners.find({
            _id: { $in: bp_predicates }
          }, {
            sort: {
              name
            }
          }).fetch();
        }

        console.log(customers);
        return customers;
  }

Template.list_customers.events({
  'click #btnSearch': function(event) {
    searchTerm = $('#customerSearch').val();
    Template.list_customers.__helpers[" getMyCustomers"](searchTerm);
    //eventsUI.changed();
  }
});

服务器代码

Meteor.publish("getCustomers",function(customerIds,limit,searchTerm){
  if(!searchTerm){
    return BusinessPartners.find({
      _id:{$in:customerIds}
    },{limit:limit});
    this.ready();
  }
  else{
    customers = BusinessPartners.find({
      _id:{$in:customerIds},
      $text:{$search:searchTerm}
    },
    {fields:{score:{$meta:"textScore"}},
     sort:{score:{$meta:"textScore"}}
    },
    {limit:limit});
    console.log('Server side ', customers.fetch(), customers.count());
    return customers;
    this.ready();
  }
});

【问题讨论】:

    标签: javascript meteor meteor-helper


    【解决方案1】:

    percolate:paginated-subscriptiondocumentation中所述:

    分页订阅希望您有一个发布设置,如 正常,它期望作为最终参数的当前数量 要显示的文件

    这里重要的词是“最终”。因此,您的发布函数应该将limit 作为最后一个参数,不是第二个:

    Meteor.publish("getCustomers",function(customerIds,searchTerm,limit) {
    

    【讨论】:

    • 感谢您指出限制的怪癖。但是屏幕还没有刷新搜索到的值,我在客户端的控制台日志输出中得到了正确的结果。
    猜你喜欢
    • 2012-06-16
    • 2017-11-30
    • 2013-09-26
    • 2013-11-16
    • 2013-12-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-02
    相关资源
    最近更新 更多