Update collection inside autorun


#1

Hello!
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!


#2

Ok, i found a pretty good way (may be not :ghost:).
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


#3

For this, what I would personally do is to run the update in the publication at the very beginning, then let the publish execute and return the correct document. This should allow you to get rid of the autorun completely.


#4

Yes, i thought about this. But, im just wanna find a decision which can help me get more control on the client. Call upViews(), for example, after setTimeot. And, i use this.autorun because im wanna know when template.subscription is ready, it is a deliberate decision:)