简体   繁体   中英

Reactive Template re-render meteor js after search

I have written a simple search through publish and subscribe, I can see the search results being displayed correctly in both the server and client with console.log, however my template does not seem to re-render itself, inspite of having a tracker dependency. Below is the code for client, publish, and the template onRendered call.

Client Code

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

Server Code

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

As stated in the percolate:paginated-subscription documentation :

The paginated subscription expects you to have a publication setup, as normal, which expects as a final argument the current number of documents to display

The important word here is "final". Therefore, your publication function should have limit as a final argument, not second:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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