为作为文档返回的每个数组创建单选按钮组

create radiobutton group for each array returned as a document

topics是其中returns几个文档的集合。每个文档都有几个字段。其中一个字段是一个字符串数组。 我试图将字符串数组的值表示为返回的每个文档的单选按钮组。我无法给每个组一个唯一的名称。我尝试了 console.log 语句,发现单选按钮组效果不错,但随后由于调用助手的次数多于返回的文档数而变得混乱。

我的html

<template name=topics>
  {{#each topics}}
    <tr>
      <td><input type="checkbox" name="selectone" id="{{uniqueid}}"/></td>
      <td><textarea rows=4 name="topicname" readonly>{{name}} </textarea></td>
      <td><input type="text" value="{{dateCreated}}" name="datecreated" readonly/></td>
      <td>
        {{#each choices}}

          {{>radiogroup}}

        {{/each}}

      </td>
      <td><a href="#" name="edittopic">Edit</a></td>
    </tr>

  {{/each}}
</template>

<template name='radiogroup'>
  <li><input type=radio name="{{radiogroupname}}" />{{this}}</li>
</template>

我的js:

Template.topics.helpers({
  uniqueid: function() {
    Session.set('topicid',this._id);
    return this._id;
  },
  choices:function() {
    return this.choices;
  },
});

Template.radiogroup.helpers({
  radiogroupname: function() {
    return(Session.get('topicid'));
  },
});

这不是对 Session 变量的良好使用,因为您要一遍又一遍地重复使用它来表示循环内的给定 _id。它将不断变化。由于您只需要父对象的 _id,您可以使用空格键中的相对路径表示法在模板中访问它,如下所示:

<template name='radiogroup'>
  <li><input type=radio name="{{../_id}}" />{{this}}</li>
</template>

那你甚至不需要你的帮手。在任何情况下你都不需要你的 choices 助手,因为 choices 已经是可迭代的了。