用 meteor.js 反应调用 js function/event
Reactively call a js function/event with meteor.js
我是 meteor.js 的新手。还是习惯了。
我了解模板如何根据服务器上的光标更新进行反应式更新,如下所示:
{{#if waitingforsomething.length}} Something Happened! {{/if}}
这有利于在页面上显示元素、更新列表和内容。现在,我的问题是:如果我想调用一些 javascript 或在响应更新时触发一些事件怎么办? meteor.js 的正确方法是什么?
它有点过时了,但是 Sacha Greif's Reactivity Basics 是对流星反应模型的非常快速和简洁的介绍。
基本上,您拥有所谓的 reactive computations
,观察特殊数据对象(sessions
、subscriptions
、cursors
等)并在任何时候执行的代码其中 reactive sources
个更改。
这是通过 Tracker API
公开的
我的计算效果很好:
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()
}
};
希望对您有所帮助。
Tracker.autorun
or template instance 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);
}
});
我是 meteor.js 的新手。还是习惯了。 我了解模板如何根据服务器上的光标更新进行反应式更新,如下所示:
{{#if waitingforsomething.length}} Something Happened! {{/if}}
这有利于在页面上显示元素、更新列表和内容。现在,我的问题是:如果我想调用一些 javascript 或在响应更新时触发一些事件怎么办? meteor.js 的正确方法是什么?
它有点过时了,但是 Sacha Greif's Reactivity Basics 是对流星反应模型的非常快速和简洁的介绍。
基本上,您拥有所谓的 reactive computations
,观察特殊数据对象(sessions
、subscriptions
、cursors
等)并在任何时候执行的代码其中 reactive sources
个更改。
这是通过 Tracker API
公开的我的计算效果很好:
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()
}
};
希望对您有所帮助。
Tracker.autorun
or template instance 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);
}
});