如何使用Meteor中的嵌套对象过滤每个循环?

时间:2015-02-22 06:51:11

标签: mongodb meteor meteor-blaze meteor-collection2

我骑自行车穿过Meteor中的嵌套对象。我需要能够过滤嵌套对象,但我不知道如何在流星中做到这一点。任何人都知道如何做到这一点?

以下是一些示例代码:

People.attachSchema(new SimpleSchema({
    name: {
        type: String,
        label: "Name",
        max: 200
    },
    importantFacts: {
      type: [Object],
      optional: true
    },
    "importantFacts.$.year": {
      type: Number,
      index: true
    },
    "importantFacts.$.content": {
      type: String
    }
}));

我正在尝试做的例子(不起作用):

<ul>
    {{#each person.importantFacts.find({ year: 2010 })}}
        <li>{{ content }} - {{ year }}</li>
    {{/each}}
</ul>

1 个答案:

答案 0 :(得分:1)

{{each}}是一个Spacebars循环结构,它采用非常简单表达式。您可以在find({...})中执行{{each}};您定义模板助手,然后将其传递给each

Template.foo.helpers({
  importantFacts: function () {
    return People.importantFacts.find({ year: 2000 });
  }
});
<template name="foo">
  <ul>
    {{#each importantFacts}}
      <li>{{ content }} - {{ year }}</li>
    {{/each}}
  </ul>
</template>

这种关注点的分离是相当基础的,所以如果您刚开始使用Meteor,可能有助于回顾一些Meteor基础知识。 Your First Meteor Application是一个很好的资源。另一方面,我看到你在一个月前发布了相当复杂的Meteor代码,所以也许我误解了你的问题?

相关问题