我正在尝试在 Meteor 空格键模板中显示一些关系数据。具体来说,我有两个系列,位置和天气。它们看起来像这样:
Location {
_id: 'full-name',
title: 'Full name',
lat: '123.456',
long: '123.456',
order: 1
}
Weather {
locName: 'full-name', // this name always matches a location _id
temperature: '41.3'
}
我想在一个页面上显示来自这两个集合的信息。这样我就可以显示每个位置的最新天气(每页有 4-20 个)。为此,我在服务器端发布了两个集合的 Mongo 请求,如下所示:
Meteor.publish('allLocations', function() {
return [
Locations.find({}, { sort: { order: 1 } }),
Weather.find({}) // The weather
]
});
然后我在我的路由器(铁路由器)中订阅了这个出版物:
Router.map(function() {
this.route('locations', {
waitOn: function () {
return Meteor.subscribe('allLocations');
}
}
});
但是,当我进入我的空格键模板时,我被卡住了。我无法弄清楚在空格键中切换集合焦点的语法。
这是我要解析的模板的伪代码,但我知道这目前不起作用。
<template name="locations">
<div class="locations-grid">
{{#each locations}}
<div class="location {{_id}}">
This is the location template
<h1>{{title}}</h1>
{{#each weather}}
<!-- Trying to pass the _id along to the weather template for filtering -->
{{> weather _id}}
{{/each}}
</div>
{{/each}}
</div>
</template>
<template name="weather">
This is the weather template
{{#with weather}}
<!-- Ideally, we've now switched contexts to the weather collection -->
<h2>Temperature: <div class="temp">{{temperature}}</div></h2>
{{/with}}
</template>
所以我的问题是,我在哪里告诉空格键将上下文切换到天气集合?如何将 _id 变量传递给天气模板,以便从集合中选择正确的数据?我知道我在这里错过了一大步,我只是不知道要检查流星空间的哪个部分。我知道我可能需要为天气模板指定订阅,但我不确定在哪里做,因为它不是真正的路线,因为它没有自己的页面。它只是作为位置模板中的子模板存在。
感谢您提供有关重组的任何提示或可能的建议。