【问题标题】:Creating relations with collections in Meteor在 Meteor 中创建与集合的关系
【发布时间】:2012-06-24 06:00:12
【问题描述】:

也许这种结构不适合 Meteor,或者我想错了。 试图在 noSQL 数据库中建立这样的关系吗?

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

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

我的拖放区模型

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

小部件模型(缩写)

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

【问题讨论】:

    标签: meteor handlebars.js


    【解决方案1】:

    我觉得你想要的看起来更像这样:

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

    助手:

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

    这有帮助吗?

    【讨论】:

    • 太棒了,成功了!无论出于何种原因,在评估模板时,我都无法理解它的价值。事实证明,我让它变得更复杂了。
    • 是的,这需要一些时间来适应,但它真的很棒。
    猜你喜欢
    • 1970-01-01
    • 2017-01-22
    • 1970-01-01
    • 2017-10-18
    • 2013-02-19
    • 2019-12-15
    • 1970-01-01
    • 2017-09-17
    • 1970-01-01
    相关资源
    最近更新 更多