0

I am cycling through nested objects in Meteor. I need to be able to filter the nested objects, but i'm not sure how to go about doing that in meteor. Anyone have an idea how to do this?

Here is some example code:

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
    }
}));

Example of what I am trying to do (does not work):

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

1 回答 1

1

{{each}}是一个空格键循环结构,它采用非常简单的表达式。你可以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 的一些基础知识可能会有所帮助。你的第一个 Meteor 应用程序是一个很好的资源。另一方面,我看到您大约一个月前发布了相当复杂的 Meteor 代码,所以也许我误解了您的问题?

于 2015-02-22T06:57:36.587 回答