查询Meteor中的MongoDB“每个上下文”嵌套

时间:2018-04-11 10:18:14

标签: mongodb meteor

示例:

<template name="list_customers">
  <p><h3>List Customers</h3></p>
  {{#each customers}}
    {{> list_customers_content}}
  {{/each}}
</template>

<template name="list_customers_content">
..display all customer data from "Customers.find({})...

{{> list_customers_content_ip}}

</template>

<template name="list_customers_content_ip">
.. display data from Customers_ip.find({}) based on #each customer object id.
</template>

Template.list_customers.helpers({
  customers() {
    return Customers.find({});
  },
});

如何在对两个不同集合的最少可能查询中完成此操作,是否有任何方法可以使用客户上下文对象ID?请写完整的例子。

Customers_ip.find()的助手应该是什么样的?

1 个答案:

答案 0 :(得分:1)

您需要将当前文档(this上下文中的#each)传递到list_customers_content模板,以便进一步处理它。

参数的名称将是访问子模板中值的名称。

示例:

{
  name: "John Doe",
  _id: "1232131",
}


<template name="list_customers">
  <p><h3>List Customers</h3></p>
  {{#each customers}}
    {{> list_customers_content customer=this}}
  {{/each}}
</template>

<template name="list_customers_content">
  <span>Name: {{customer.name}}</span><!-- John Doe -->
  {{> list_customers_content_ip customerId=customer._id}}
</template>

<template name="list_customers_content_ip">
  {{#each customerIp customerId}}
    <span>IP: {{this.ip}}</span>
  {{/each}}
</template>

customerIp的助手可能如下所示:

Template.list_customers_content_ip.helpers({

  customerIp(customerId) {
    // assumes, that these documents have
    // a field named "customerId"
    return Customers_ip.find({ customerId }); 
  },

});