我是meteor.js的新手。还是习惯了。我了解模板是如何根据服务器上的游标更新主动更新的,如下所示:
{{#if waitingforsomething.length}} Something Happened! {{/if}}这有利于在页面上显示元素、更新列表和内容。现在,我的问题是:如果我想调用一些javascript或者在某些事情被动态更新时触发一些事件,该怎么办?用meteor.js做这件事的正确方法是什么?
发布于 2016-08-18 12:40:21
Tracker.autorun或this.autorun中的任何内容都会随这些autoruns中的反应性数据源的变化而运行。
反应性数据源是ReactiveVar实例、db查询、Session变量等。
Template.myTemplate.onCreated(function() {
// Let's define some reactive data source
this.reactive = new ReactiveVar(0);
// And put it inside this.autorun
this.autorun(() => console.log(this.reactive.get()));
});
Template.myTemplate.events({
// Now whenever you click we assign new value
// to our reactive var and this fires
// our console.log
'click'(event, template) {
let inc = template.reactive.get() + 1;
template.reactive.set(inc);
}
});发布于 2016-08-16 07:15:10
发布于 2016-08-16 14:43:27
计算对我来说很好:
Template.myTemplate.onRendered(function() {
this.computation = Deps.autorun(function () {
if (something) {
$(".reactive").html("Something Happened!");
}
});
});
Template.myTemplate.destroyed = function(){
if (this.computation){
this.computation.stop()
}
};我希望这能帮到你。
https://stackoverflow.com/questions/38968326
复制相似问题