在 Meteor 中的嵌套#each 块之间传递数据

Passing data between nested #each block in Meteor

这是我的模板的样子

<Template name="launcherBody">
{{#each workspace in Workspace}}
    <h1>
        Workspace: {{workspace.title}}
    </h1>
    {{#each board in Boards}}
        <div class="board">
            <h2>
                {{board.title}}
            </h2>   
        </div>
    {{/each}}
{{/each}}

还有帮手

Template.launcherBody.helpers({
  workspace: function () {
      return Workspace.find({ "member_id": Meteor.userId()});
  },
  boards: function () {
    /*return Board.find({"workspaceId": ??? }) */
  }
});

因此,正如您所看到的,Board 已映射到 Workspace,要找到 Board,需要从之前的 #each.

中获取 workspace._id

我怎样才能做到这一点?

我已尝试 Template.parentData() 将 Board 和 Workspace 代码保留在不同的模板中,但问题并没有解决

您可以使用 Template.parentData(n) docs

来引用父数据上下文
boards: function () {
  var wId = Template.parentData(1)._id;
  return Board.find({"workspaceId": wId})
}

或者,您可以显式地将变量作为参数传递给助手:

{{#each board _id}}

在该表达式中 _id 将来自父项

然后您的助手将直接使用该参数:

boards: function (wId) {
  return Board.find({"workspaceId": wId})
}