在Meteor中创建与集合的关系

时间:2012-06-21 22:24:38

标签: meteor handlebars.js

也许这种结构不适合Meteor,或者我认为它错了。 试图在noSQL db中做这样的关系吗?

我有Dropzone和Widget集合。 Dropzone可以有许多小部件,每个小部件可以存在于多个dropzone中。

我的问题是我似乎无法让Handlebars呈现已过滤的小部件列表。

我的dropzone模型

dropzone =
  _id: "area1-id"
  title: "Area 1"

Widget模型(缩写)

widget =
  _id: "widget1-id"
  title: "My Widget"
  dropzones: ['area1-id', 'area2-id'] 
  # each widget stores an id of which dropzones it's associated with

相关模板结构

{{#each dropzones}}
  <div class="dropzone span4">
    <h1>{{title}}</h1>
    <div class="widget-area">
      <div class="hotzone">
        {{#widgets _id}} # passing in the current dropzone id
        {{/widgets}}
      </div>
    </div>
  </div>
{{/each}}

辅助功能

# returns the correct sets of widgets, but can't figure
# out how to make it render the widget partial
Handlebars.registerHelper 'widgets', (drop_id)->
  widgets = CC.Widgets.find(dropzones: drop_id)
  _.each widgets, (widget)->
    Template.widget(widget)  # this ends up being blank with no error

1 个答案:

答案 0 :(得分:5)

我认为你想要的东西看起来更像这样:

  <div class="hotzone">
    {{#each widgets}}
      {{> widget}}
    {{/each}}
  </div>

助手:

Template.foo.widgets = -> CC.Widgets.find(dropzones: this._id)

这有帮助吗?