我找到了流星变量观察器,我这样使用它:
Template.restaurantMap.onCreated(function() {
this.location = ReactiveVar;
this.watch("location", function(value){
console.log('value changed', value);
});
});
Run Code Online (Sandbox Code Playgroud)
它完美地运作.但是,有没有Meteor的方式来观看ReactiveVar?
我只需要观看一个ReactiveVar,而不是整个附加到Template的列表.而且我需要与Meteor的模板,助手等分开观看.
如果变量发生变化,我需要自己回调.
您可以使用自动运行,这是一种内置方式来创建自定义反应上下文(每当反应变量发生变化时运行的函数).这是一个例子:
Template.restaurantMap.onCreated(function() {
this.location = new ReactiveVar;
var self = this;
this.autorun(function() {
return console.log('location changed!', self.location.get());
})
});
Run Code Online (Sandbox Code Playgroud)