![](/img/trans.png)
[英]Why does firebase “on” “value” not run the listener function when the reference doesn't exist, but “once” “value” does?
[英]Why doesn't this function run when the reactive variable changes value?
我是新来的流星,我想抓住整个反应性事物。
没有特定的原因为什么我要重新运行此函数,实际上,不重新运行实际上是我的用例所需的行为。 我只想知道为什么会这样,所以我可以更好地理解概念。
如果我将函数作为属性添加到模板实例上,则如下所示:
Template.services.onCreated( function() {
this.templates = [
"web_design",
"painting",
"gardening"
];
this.current_index = new ReactiveVar(0);
this.determineSlideDirection = function() {
console.log(this.current_index.get());
};
});
然后我响应某些事件来更新反应性var。
Template.services.events({
'click .nav-slider .slider-item': function(event, template) {
var new_selection = event.currentTarget;
template.current_index.set($(new_selection).index());
}
});
调用set()
时不会重新运行该函数。
但是,如果我有一个使用该变量的助手 ,它将被重新运行。
Template.services.helpers({
currentTemplate: function() {
var self = Template.instance();
return self.templates[self.current_index.get()];
}
});
为什么是这样?
反应性数据源只会导致某些功能自动重新运行。 这些功能是:
Tracker.autorun
Template.myTemplate.helpers({})
Blaze.render
和Blaze.renderWithData
在上面的代码中,您需要使用Tracker.autorun
Template.services.onCreated( function() {
this.templates = [
"web_design",
"painting",
"gardening"
];
this.current_index = new ReactiveVar(0);
Tracker.autorun(function(){
// actually, this might not work because the context of
// 'this' might be changed when inside of Tracker.
this.determineSlideDirection = function() {
console.log(this.current_index.get());
};
});
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.