我刚开始研究一个 meteorite 包,它可以将组/标题添加到列表中。
See demo here .
Source code for demo is here .
基本上,我想做的是等待列表完全填充,然后调用一个方法来设置标题/组等。我现在的方法是在 { {#each}}
循环,像这样:
<ul>
{{#each listItems }}
{{> listItem }}
{{/each}}
</ul>
{{ groupList }}
groupList
帮助程序在循环运行后执行我想要运行的代码(设置标题的代码)。
这感觉有点难看,我想我更喜欢的是对 {{#each}}
循环的回调,它在完成迭代后被调用(和 重要的是 如果循环中的项目发生变化)。
我猜有点像模板的 rendered() 方法。
(我当然已经尝试过 rendered() 方法,但它在循环运行之前就已执行。)
有这样的回调吗?或者其他一些比我目前使用的 Handlebars 助手更不丑陋的技术?
最佳答案
与其让 Blaze 呈现列表然后使用 JQuery 添加标题,不如在模板本身中进行分组和标题,如下所示:
<template name="grouped">
{{#each groups}}
<h2>{{heading}}</h2>
{{#each items}}
{{> item}}
{{/each}}
{{/each}}
</template>
Template.grouped.groups = function () {
return _.chain(ListItems.find().fetch())
.groupBy("wins")
.pairs()
.map(function (pair) {return {heading: pair[0], items: pair[1]};})
.value();
};
这对我来说似乎更“ meteor ”——命令式 DOM 操作在 Meteor 中通常是错误的方法。我不确定性能比较如何。
关于javascript - meteor .js : Execute method after list has been populated,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24427627/