[英]Reactively call a js function/event with meteor.js
我是meteor.js的新手。 仍然习惯它。 我了解了如何根据服务器上的游标更新来反应性更新模板,如下所示:
{{#if waitingforsomething.length}} Something Happened! {{/if}}
这对于在页面上显示元素,更新列表和内容很有用。 现在,我的问题是:当我要响应性地更新某些内容时,如果我想调用一些JavaScript或触发某个事件该怎么办? 使用meteor.js的正确方法是什么?
这有点过时了,但是Sacha Greif的“ 反应性基础知识”是对流星反应性模型的快速而简洁的介绍。
基本上,您具有所谓的reactive computations
,该代码观察特殊的数据对象( sessions
, subscriptions
, cursors
等),并且只要这些reactive sources
任何一个发生更改,代码便会执行。
这是通过Tracker API公开的
在Tracker.autorun
或模板实例中的任何内容, Tracker.autorun
都会在Tracker.autorun
this.autorun
运行中的反应性数据源发生更改的情况this.autorun
运行。
反应性数据源是ReactiveVar
实例,数据库查询, 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);
}
});
计算对我来说效果很好:
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()
}
};
我希望这有帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.