自动运行内更新集合,无需重新运行(Meteor.JS)
问题描述:
我试图找到最好的实践,我们如何更新内部跟踪器authorun;自动运行内更新集合,无需重新运行(Meteor.JS)
例如,我想更新我收藏中的视图数。但我想确保我的订阅已准备就绪,并且我有工作的对象;
例如在onCreated部分:
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();
}
});
此代码将变为无限循环,因为我们注释掉的代码将改变对象。在这种情况下如何做到最好?非常感谢你!
答
好吧,我发现了一个很不错的方法(可能不是)。 我们可以通过非反应更新收藏追踪功能();
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();
})
}
});
我认为就是这样。也许有人会提供更好的? 如果没有优惠版本更好,我将在一天内关闭该问题
这就是实现它的方法。 –
@MichelFloyd你好!谢谢! –