简体   繁体   English

如何在流星模板助手之间传递数据?

[英]How to pass data between Meteor Template Helpers?

Meteor Newbie here! 新手流星在这里!

I have a page where all the open orders are displayed. 我有一个页面,其中显示所有未结订单。 The order details are stored in a collection. 订单详细信息存储在集合中。 A Template helper will return the order details. 模板助手将返回订单详细信息。

Template.delivery.helpers({
  openOrders: function(){
   return Orders.find({status: "open"});
  }
});

The template look some what like this. 模板看起来像这样。

{{#if openOrders}}
  {{#each openOrders}}
     Date: {{createdAt}}
     Total items: {{items.length}}
     Location: {{location}} //It prints the location _id
  {{/each}}
{{/if}}

The Order Collection only have the _id of the location. 订单集合只有位置的_id。 The Location details are stored in a Collection named Locations . 位置详细信息存储在名为Locations的集合中。

I want to display the location name (which is stored in the Location collection) instead of the _id. 我想显示位置名称(存储在Location集合中)而不是_id。

I created a Template helper which returns the Location details, but how can I link these to helpers so that the Location name is displayed instead of Location _id ? 我创建了一个模板助手,该助手返回“位置”详细信息,但是如何将它们链接到助手,以便显示“ Location name而不是“ Location _id

当您以关系数据库方式使用mongodb时,您需要安装publish-composite包,以确保所有必需的数据都已订阅。

When you use each , it will set the this to the current thing that is being iterated over. 当您使用each ,它将把this设置为正在迭代的当前对象。 This will allow you to use this in your helper to perform lookups. 这将允许您在助手中使用this来执行查找。 So in this case, if you're using a helper to get the orders: 因此,在这种情况下,如果您使用帮助程序来获取订单:

orders: function () {
  return Orders.find({ status: "orders" });
}

Then when you iterate over it with {{#each}} , this is set to the current order, meaning your location helper with look like this: 然后,当您使用{{#each}}对其进行迭代时, this被设置为当前顺序,这意味着您的位置帮助器如下所示:

location: function () {
  return Locations.findOne(this.locationId);
}

Putting it all together in the template it would be like: 将所有内容放到模板中,就像:

{{#if Template.subscriptionsReady}}
  {{#each orders}}
    <h1 class="title">Order #{{_id}}</h1>
    {{#with location}}
      <div class="location">
        <span>Latitude: {{ latitude }}</span>
      </div>
    {{/with}}
  {{/each}}
{{/if}}

Keep in mind: this will only work if you also publish your locations . 注意事项: 仅当您还发布位置时,此方法才有效

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM