0

I can't seem to get reactive collection changes from server side using the following code:

Template.name.onRendered(function() {
    this.autorun(function(){
            Meteor.subscribe("items", Session.get("start"), Session.get("end"), function () {
             //update UI
            });
    });
});

When Session.get("start") changed, it will subscribe all the items within the range from server correctly, but if I delete a item from mongo shell, the subscription callback won't be called, i.e. that re-subscription is not triggered at client side, I need to get these two conditions working,

  1. WHEN Session.get("start"), Session.get("end") changed THEN get data within range from server(I do this with subscribe) and update UI
  2. WHEN the query result Items.find(start, end) changed (server side changes to the collection) THEN also update UI

I can almost get these working by using another autorun to track the Items.find() query,

    Template.name.onRendered(function() {
        // when user changed the date range, get the new data
        this.autorun(function(){
                Meteor.subscribe("items", Session.get("start"), Session.get("end"), function () {
                 //update UI
                });
        });
        // get all the changes from server and update UI
        this.autorun(function(){
               Items.find(start, end);
               //update UI   
        });
    });

but seems after each subscription completes, the query autorun also runs, which is not necessary, it's really supposed to only pick changes from server, what should I do to reflect any changes within range at server side to the client?

Sawyer
  • 15,581
  • 27
  • 88
  • 124
  • The subscription will only run again if your reactive variables (start and end) change. You are overworking your subscription callback in order to get UI updates (this isn't the right pattern to use). Your solution should probably look something more like [this](https://stackoverflow.com/questions/31187876/how-do-i-run-client-side-code-after-a-new-collection-item-has-been-added-to-a-me). – David Weldon Jul 03 '15 at 06:14
  • @DavidWeldon the data needed by the UI only available after the subscription completes. I'm curious to know why this is not the right pattern, thanks! – Sawyer Jul 03 '15 at 06:28
  • What exactly is "update UI" though? And why are you actively updating it rather than having it update on its own reactively? You may be taking the wrong approach here. – CaptSaltyJack Jul 03 '15 at 07:13
  • @CaptSaltyJack it is updating the fullcalendar events source, it is a js library i can only fresh it by calling its API – Sawyer Jul 03 '15 at 07:25
  • Have you considered using Tracker.Dependency to make the calendar calls reactive? – CaptSaltyJack Jul 03 '15 at 07:36

0 Answers0