【问题标题】:Why does a helper rerun when not referenced to any reactive variables inside a #with-block?当没有引用#with-block 中的任何反应变量时,为什么助手会重新运行?
【发布时间】:2015-09-03 19:29:35
【问题描述】:

为什么#with 块内的帮助器insideWithfoo 更改时会重新计算?助手没有引用任何响应式数据源!

foo.js

var foo;

if (Meteor.isClient) {
  foo = new ReactiveVar();
  foo.set(0);
  Template.board.helpers({
    obj: function() {
      return {
        foo: foo.get()
      };
    },
    insideWith: function() {
      return console.log('insideWith');
    },
    fooInsideWith: function() {
      foo.get();
      return console.log("fooInsideWith ");
    },
    notInsideWith: function() {
      return console.log('notInsideWith');
    },
    fooNotInsideWith: function() {
      foo.get();
      return console.log("fooNotInsideWith ");
    }
  });
  Template.board.events({
    'click button': function(e, t) {
      return foo.set(Math.floor(Math.random() * 10));
    }
  });
}

foo.html

<body>
    {{> bar}}
</body>

<template name="bar">
    <div class="bar">
        <button>Random</button>
        <div>
            {{#with obj}}
                {{insideWith}}
                {{fooInsideWith}}
            {{/with}}
        </div>

        <div>
            <span>{{obj.foo}}</span>
            {{notInsideWith}}
            {{fooNotInsideWith}}
        </div>
    </div>
</template>

【问题讨论】:

  • 嗯? foo 是一个反应式数据源。 foo = new ReactiveVar();
  • 是的,但 insideWith 不使用它。它只打印到控制台。
  • 你有{{#with obj}}obj 是您定义的帮助器,它根据反应 var foo 返回某些内容
  • 这与insideWith 助手有什么关系?请详细说明!
  • {{#with}} 将在obj 更改时自动运行,因为obj 是响应式数据源foo 的助手。 {{insideWith}} 嵌套在 {{#with}} 中,所以显然它也可以运行。

标签: javascript meteor spacebars


【解决方案1】:

来自 cmets (@fuzzybabybunny):

{{#with}} 将在 obj 更改时自动运行,因为 obj 是响应式数据源 foo 的助手。 {{insideWith}} 嵌套在 {{#with}} 中,所以显然它也可以运行。

【讨论】:

    猜你喜欢
    • 2016-05-23
    • 2015-11-10
    • 2021-04-06
    • 2021-01-23
    • 1970-01-01
    • 2015-10-14
    • 2020-06-21
    • 2017-09-29
    • 1970-01-01
    相关资源
    最近更新 更多