Search code examples
meteormeteor-blazeflow-router

Update collection inside autorun without rerun (Meteor.JS)


I trying to find best practies how can we update collection inside tracker authorun;

For example i want to update count of views in my collection. But i wanna be sure my subscription is ready and i have object for work;

For example in onCreated section:

 var self = this;
    self.autorun(function() {
        var topicId = FlowRouter.getParam('_id');
        self.subscribe('Topic', topicId);
        if(self.subscriptionsReady()){
          //  var topic = Topic.findOne(topicId);
          //  topic.upViews();
        }
    });

This code will goes in infinite loop, because our commented out code will change object. How best to do in this case ? Thank you very much!


Solution

  • Ok, i found a pretty good way (may be not). We can update a collection with nonreactive Tracker function();

    var self = this;
    self.autorun(function() {
        var topicId = FlowRouter.getParam('_id');
        self.subscribe('Topic', topicId);
        if(self.subscriptionsReady()){
          Tracker.nonreactive(function(){
            var topic = Topic.findOne(topicId);
            topic.upViews();
          })
      }
    });
    

    I think that 's it. Maybe someone will offer better? If no offer version is better , I 'll close the question in a day