我有一个表示评论流的 JSON,例如:
var comments = [
{ "comment_id": 1, "author_id": 100, "text": "hello world!" },
{ "comment_id": 2, "author_id": 120, "text": "cheers world!" },
{ "comment_id": 3, "author_id": 100, "text": "goodmorning world!" },
{ "comment_id": 4, "author_id": 100, "text": "goodnight world!" } ]
您可能会注意到,author_id
100 人发表了 3 次评论。
我author
的 s 数据有另一个 JSON,例如:
var authors = {
100: {"username": "ciccio"},
120: {"username": "pernacchia"} }
我想在渲染模板时加入这两个 JSON,authors
使用author_id
as 键查找。有这样的助手会很棒:
<div class="comments">
<!-- `join` takes an iterable, a lookup dict, a key,
and a new name for the joined property -->
{{#join comments authors "author_id" "author"}}
<div class="Comment">
{{author.username}} says: {{text}}
</div>
{{/join}}
</div>
我试图写下一个助手来做到这一点,但我不明白如何在助手中引用authors
字典。
现在我正在手动加入这两个字典,为模板创建一个临时的和新的 JSON。但是将这项工作委托给模板会很棒。