Javascript 是否可以在Meteor spacebars模板中切换收集焦点?

Javascript 是否可以在Meteor spacebars模板中切换收集焦点?,javascript,mongodb,meteor,spacebars,Javascript,Mongodb,Meteor,Spacebars,我试图在Meteor spacebars模板中显示一些关系数据。具体来说,我有两个系列,地点和天气。它们看起来像这样: 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 tempera

我试图在Meteor spacebars模板中显示一些关系数据。具体来说,我有两个系列,地点和天气。它们看起来像这样:

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'
}
Template.locations.helpers({
  locations: function() {
    return Locations.find();
  }
});
Template.location.helpers({
  weathers: function() {
    return Weather.find({locName: this._id});
  }
});
我想在一个页面上显示这两个集合中的信息。因此,我可以显示每个位置的最新天气,每页有4-20个。为此,我在服务器端发布了两个集合的Mongo请求,如下所示:

Meteor.publish('allLocations', function() {
    return [
        Locations.find({}, { sort: { order: 1 } }),
        Weather.find({}) // The weather 
    ]    
});
然后,我在我的路由器iron router中订阅本出版物:

Router.map(function() {
    this.route('locations', {
        waitOn: function () {
            return Meteor.subscribe('allLocations');
        }
    }
});
然而,当我到达我的空格键模板时,我被卡住了。我无法理解在空格键中切换集合焦点的语法

这是我试图解析的模板的psuedo代码,但我知道这目前不起作用

<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>
所以我的问题是,我在哪里告诉spacebars将上下文切换到天气集合?如何将_id变量传递给天气模板,以便从集合中选择正确的数据?我知道我在这里错过了一大步,我只是不知道该检查流星空间的哪一部分。我知道我可能需要为天气模板指定订阅,但我不确定在哪里做,因为它不是真正的路线,因为它没有自己的页面。它只是作为“位置”模板中的子模板存在


感谢您提供有关重组的任何提示或建议。

在我们开始之前,请阅读-这将正确引导您了解每个区块内的上下文

您的目标是将正确的天气文档连接到其相应的位置文档。通过为这两种类型引入子模板,最容易实现这一点。让我们从顶层模板开始:

<template name="locations">
  <div class="locations-grid">
    {{#each locations}}
      {{> location}}
    {{/each}}
  </div>
</template>
<template name="location">
  <div class="location">
    <h1>{{title}}</h1>
    {{#each weathers}}
      {{> weather}}
    {{/each}}
  </div>
</template>
接下来,位置模板:

<template name="locations">
  <div class="locations-grid">
    {{#each locations}}
      {{> location}}
    {{/each}}
  </div>
</template>
<template name="location">
  <div class="location">
    <h1>{{title}}</h1>
    {{#each weathers}}
      {{> weather}}
    {{/each}}
  </div>
</template>
这里的关键洞察是位置模板的上下文是单个位置文档,因此weather将仅返回此位置实例的天气文档。最后,您的天气模板可以如下所示:

<template name="weather">
  <h2>Temperature: {{temperature}}</h2>
</template>
注意,我们现在处于天气环境中,因此不再需要with


旁注-在本例中,在您的发布者中使用排序具有重要意义。

谢谢。这很有帮助。我还阅读了《发现流星》的链接帖子。我还是有点被一个变化难住了。为此,我只能将位置集合发布到位置模板。如何告诉位置模板要订阅哪个集合?我习惯在路由器上这样做,但是由于这个位置没有路由,我不确定订阅会去哪里。看起来你不需要更改任何东西。订阅不是特定于模板的,而是请求服务器将数据推送到客户机数据库minimongo。当您导航到顶级模板时,您的路由器订阅所有位置,因此所有天气和位置数据将可用于所有模板。这有意义吗?当我在allLocations出版物中保留位置和天气集合时,每个天气都会将其作为Locations对象返回,但也包括天气条目。修复了!问题不在出版物中,而是重用天气作为助手和模板名。让我知道更新的代码是否适合您。如果不是的话,我有一份回购协议来证明我可以发布的所有这些。呼!这就解决了问题。我看到集合光标和子模板之间的区别。非常感谢您花时间解释这一点,并与我详细讨论细节。