Amc_rtty
Amc_rtty

Reputation: 3813

Updating already published collection in Meteor

I'm trying to understand how to REpublish in Meteor. I have this published "allCustomers" data which gets loaded initially on page load, with the customers from current month:

Meteor.publish("allCustomers", function () {

  var pDate = moment().utc().toDate();
  var dateFilter = GetWholeMonthFilterFromDate(pDate);      
  return Customers.find(dateFilter); 
});

All good so far. I also have this server method that gets customers when the user selects a different month than the current one:

Meteor.methods({
    allCustomers:function(pDate){

    function GetWholeMonthFilterFromDate(pDate){
      var firstDayOfMonth = new Date(pDate.getFullYear(), pDate.getMonth(), 1);
      var lastDayOfMonth = new Date(pDate.getFullYear(), pDate.getMonth() + 1, 0);

      var dateFilter = 
      {
        "joined": {"$gte": firstDayOfMonth, "$lte": lastDayOfMonth} 
      };
      return dateFilter;
    }

    var dateFilter = GetWholeMonthFilterFromDate(pDate);      
    return Customers.find(dateFilter).fetch(); 
},

I use this "allCustomers" server method like this:

Meteor.call("allCustomers", selectedDate, function(error, result){
        if(error)
        {
            console.log('ERROR :', error);
            return;
        }
        else
            console.log(result);
});

My question is, when the user selects a different date, how do I use the resultset from the Meteor.call("allCustomers") to update the initial published "allCustomer" data? Meaning, even if I do see the new customers in the console (as the callback executes), my page is still using the old data from the initial publishing. How do I re-update the published data with the result of the Meteor.call() ?

Thanks in advance

Upvotes: 0

Views: 132

Answers (1)

Kuba Wyrobek
Kuba Wyrobek

Reputation: 5273

Try to resubscribe to allCustomers, which needs little improvement:

Meteor.publish("allCustomers", function (pDate) {
  var dateFilter = GetWholeMonthFilterFromDate(pDate);      
  return Customers.find(dateFilter); 
});

Your allCustomers publish function was modified by adding parameter pDate which is much more flexible than creating pDate inside.

In this approach you stop subscription and subscribe again with param pDate.

// UPDATED after @saimeunt comment 

if (Meteor.isClient) {
    Meteor.startup(function(){
        Tracker.autorun(function(){
          Meteor.subscribe('allCustomers',Session.get("selectedDate"));      
        })
        // initial selection
        Session.set("selectedDate",moment().utc().toDate());
    })
}

Remember to change date in Session.set("selectedDate", DATE_PARAM) menu template:

Template.tpl_name.events({
  'click .month':function(e, tpl){
    var DATE_PARAM = ... ;
    Session.set("selectedDate", DATE_PARAM)
  }
})

Everytime user will click on .month button Session.get("selectedDate") will be changed and that will cause resubscription.

Example showing this approach

Upvotes: 2

Related Questions