简体   繁体   English

流星中的辅助功能是否具有反应性?

[英]Are helper functions in Meteor reactive?

Suppose I have the following code in my HTML file: 假设我的HTML文件中包含以下代码:

{{#if someVar}}
    {{>someTemplate}}
{{/if}}

Now, if initially someVar returns false then the someTemplate Template is not created. 现在,如果最初someVar返回false则不会创建someTemplate模板。 Now, due to some event, the value returned by someVar is true will the template be rendered or not? 现在,由于某些事件, someVar返回的值是true是否将渲染模板? Do I need to make someVar a ReactiveVar for this to work? 我需要使someVar成为ReactiveVar才能起作用吗? Or should I use autorun ? 还是应该使用autorun Which is the best and easiest method to do this? 哪种方法最方便,最简便? This can be achieved quite easily with 2-way binding in Angular. 使用Angular中的2向绑定可以很容易地实现这一点。

Considering the given template above: 考虑上面给定的模板:

{{#if someVar}}
    {{>someTemplate}}
{{/if}}

Non-Reactive Example 非反应性示例

var someNumber = 42; // changing this will not cause a new run

Template.theTemplate.helpers({
  someVar() {
    return someNumber;
  }
});

Reactive Example 1 反应性示例1

var someNumber = new ReactiveVar(42); // changing this will cause a new run

Template.theTemplate.helpers({
  someVar() {
    return someNumber.get();
  }
});

Reactive Example 2 反应性示例2

Template.theTemplate.onCreated(function onCreated() {
  this.someNumber = new ReactiveVar(42); // changing this will cause a new run
});

Template.theTemplate.helpers({
  someVar() {
    return Template.instance().someNumber.get();
  }
});

The forementioned 2-way binding is achieved automatically using ReactiveVar oder ReactiveDict. 前面提到的2向绑定是使用ReactiveVod或ReactiveDict自动实现的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM