我正在尝试实现可扩展的帖子评论的层次结构,例如Quora,以便用户可以单击评论并查看任何回复.
为此,我想跟踪每个"注释"模板实例是否"展开",切换事件处理程序中的状态.
我可以使用整个堆栈会话变量(即每个注释一个)来执行此操作,但这看起来很笨,因为任何给定页面上都有任意数量的注释.
下面是我正在尝试的内容片段.
JS:
Template.comment_item.events = {
'click #comment-content': function( e, instance ) {
this.expanded = true; // also tried instance.data.expanded = true
}
};
Template.comment_item.helpers({
showChildComments: function(){
this.expanded;
}
});
Run Code Online (Sandbox Code Playgroud)
HTML:
<template name="comment_item">
<li class="comment comment-displayed" id="{{_id}}">
<div class="comment-body">
<div id="comment-content">
<!-- some comment data here -->
</div>
{{#if showChildComments}}
<ul class="comment-children comment-list">
{{#each child_comments}}
{{> comment_item}}
{{/each}}
</ul>
{{/if}}
</div>
</li>
</template>
Run Code Online (Sandbox Code Playgroud)
不幸的是,当我单步执行时,似乎在showChildComments帮助器中,模板实例无法看到扩展变量.我在文档中注意到它说instance.data只在事件映射中读取.
有没有办法直接在事件映射中修改模板实例?