为什么foo
更改时,#with
-block 内的帮助程序insideWith
会被重新计算?帮助程序未引用任何反应式数据源!
傅.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));
}
});
}
傅.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>
来自注释 (@fuzzybabybunny):
每当 OBJ 更改时,{{#with}} 将自动运行,因为 Obj 是反应式数据源 foo 的帮助程序。{{insideWith}} 嵌套在 {{#with}} 中,所以显然它也在运行。