我目前有一个模板,其中包含{{#each}}
在其中循环。我正在尝试找到一种方法来触发特定功能{{#each}}
循环已完成。Template.rendered
仅在第一次渲染模板时运行,因此不幸的是,这不起作用。
有什么可以做到这一点吗?
我就是这样做的:
Template.foo.rendered=function(){
// NEW in 0.8.3, use this.computation=Deps.autorun and
// this.computation.stop() in destroyed callback otherwise
this.autorun(function(){
var cursor=Foo.find({/* same query you feed the #each with */});
cursor.forEach(function(foo){
// transformations on the updated model ?
// this is important to call forEach on the cursor even if you don't do
// anything with it because it actually triggers dependencies on documents
});
NEW in 0.9.1, use Deps otherwise
Tracker.afterFlush(function(){
// here you are guaranteed that any DOM modification implied by the
// each loop is finished, so you can manipulate it using jQuery
this.$(".foo-item").doStuff();
}.bind(this));
}.bind(this));
};
此代码设置模板本地自动运行(当从 DOM 中删除模板时自动停止计算)以跟踪通过光标对集合所做的更改(使用forEach
) 使用与 #each 参数相同的查询。
每当数据库被修改时,它都会再次运行,如果愿意的话,您可以迭代修改后的文档。
数据库被修改,它也会使计算设置无效#each
阻止并执行 DOM 元素插入/修改/删除。
在创建的模板计算中this.autorun
,我们不确定 DOM 操作是否已经发生,这就是为什么我们使用Tracker.afterFlush
在 DOM 再次冻结后运行代码。
如果每次 #each 失效后必须触发的代码段不使用 DOM,那么您可以忘记这一点Tracker.autoFlush
东西,但我认为确实如此。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)