Javascript 当未引用#with块中的任何反应变量时,为什么帮助程序会重新运行?
当Javascript 当未引用#with块中的任何反应变量时,为什么帮助程序会重新运行?,javascript,meteor,spacebars,Javascript,Meteor,Spacebars,当foo更改时,为什么在#with-块中的insideWith帮助程序会重新计算?助手未引用任何被动数据源 foo.js var foo; if (Meteor.isClient) { foo = new ReactiveVar(); foo.set(0); Template.board.helpers({ obj: function() { return { foo: foo.get() }; }, insideWi
foo
更改时,为什么在#with
-块中的insideWith
帮助程序会重新计算?助手未引用任何被动数据源
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>
{{>bar}
随机的
{{{带obj}}
{{insideWith}}
{{fooinewith}
{{/与}}
{{obj.foo}}
{{notInsideWith}}
{{fooonotinsidewith}}
来自评论(@fuzzybabbunny):
每当obj发生变化时,{{#with}}将自动运行,因为obj是被动数据源foo的助手。{{insideWith}}嵌套在{{{with}的内部,所以显然它也运行。
foo
是一个反应式数据源foo=newreactivevar()代码>是,但insideWith不使用它。它只打印到控制台。您有{{{with obj}}
obj
是您定义的一个助手,它根据反应式varfoo
返回一些东西,这与内部的助手有什么关系?请详细说明<当obj
发生更改时,code>{{{with}}
将自动运行,因为obj
是被动数据源foo
的助手{{insideWith}
嵌套在{{{with}}
的内部,因此显然它也可以运行。