【发布时间】:2014-06-20 08:49:45
【问题描述】:
我正在构建一个聊天应用程序,在我的“新聊天”页面上,我有一个联系人列表,您可以通过点击它们来一一选择(在此基础上,我应用了一个 CSS 选择的类并将用户 ID 推送到名为“newChatters”的数组。
我想让这个数组可用于辅助方法,这样我就可以显示一个反应式名称列表,其中包含已添加到聊天中的所有用户。
我想在其中显示响应式列表的模板:
<template name="newChatDetails">
<div class="contactHeader">
<h2 class="newChatHeader">{{newChatters}}</h2>
</div>
</template>
选择联系人时触发的点击contactItem事件:
Template.contactsLayout.events({
'click #contactItem': function (e) {
e.preventDefault();
$(e.target).toggleClass('selected');
newChatters.push(this.username);
...
newChatters 数组正在正确更新,因此到目前为止一切正常。现在我需要让 {{newChatters}} 响应式更新。这是我尝试过的方法,但它不正确并且不起作用:
Template.newChatDetails.helpers({
newChatters: function() {
return newChatters;
}
});
如何以及在哪里使用 Deps.autorun() 来完成这项工作?我什至需要它,因为我认为辅助方法会在失效时自动更新?
【问题讨论】:
标签: javascript meteor