繁体   English   中英

搜索后反应模板重新渲染流星js

[英]Reactive Template re-render meteor js after search

我已经通过发布和订阅编写了一个简单的搜索,我可以看到使用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();
  }
});

如在percolate:paginated-subscription 文档中所述

分页订阅期望您像往常一样具有发布设置,该发布设置希望当前显示的文档数量作为最终参数

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM