Blaze 模板,遍历字段?

Blaze template, loop through fields?

我一直无法找到任何可用于循环遍历名为“week01”、“week02”、“week03”的字段的快捷方式的引用,我可以在其中引用作为包含子字段的对象的字段在相同的结构中。

我知道每个文档的#each,但不知道如何处理每个字段。我有 30 周的字段,想遍历这些字段,然后也引用对象属性。

任何人都可以给我提示或可能对我有帮助的在线资源吗?我是一名 VB/php 开发人员,正在开发我的第一个 Meteor 应用程序,并且是第一次使用 Mongo。希望我遗漏了一些可能的东西。

    "personId": "rY7XaJJkrdAWaByQK",
    "week01": {
        "date": {
            "$date": "2021-04-07T12:00:00.000Z"
        },
        "field1": "chunks as necessary",
        "field2": "readable English",
        "field3": "Contrary to popular belief"
    },
    "week02": {
        "date": {
            "$date": "2021-04-14T12:00:00.000Z"
        },
        "field1": "consectetur, from",
        "field2": "more recently with desktop",
        "field3": "Contrary to popular belief"
    },
    "week03": {
        "date": {
            "$date": "2021-04-21T12:00:00.000Z"
        },
        "field1": "going through",
        "field2": "readable English",
        "field3": "Contrary to popular belief"
    },
    "week04": {
        "date": {
            "$date": "2021-04-28T12:00:00.000Z"
        },
        "field1": "words which don't look",
        "field2": "sure there isn't",
        "field3": "Contrary to popular belief"
    },
    "week05": {
        "date": {
            "$date": "2021-05-05T12:00:00.000Z"
        },
        "field1": "only five centuries",
        "field2": "Where can I get some",
        "field3": "infancy. Various"
    }
} ```

#each 仅接受数组或游标,因此您需要从对象中提取数组。

在 JavaScript 中,您可以从对象(即您的文档)的字段(属性)生成一个数组,并过滤​​它们以仅包含 week 在其 属性 名称(键)。然后你可以 return 他们通过 helper:

const document = {
   "personId": "rY7XaJJkrdAWaByQK",
    "week01": {
        "date": {
            "$date": "2021-04-07T12:00:00.000Z"
        },
        "field1": "chunks as necessary",
        "field2": "readable English",
        "field3": "Contrary to popular belief"
    },
    "week02": {
        "date": {
            "$date": "2021-04-14T12:00:00.000Z"
        },
        "field1": "consectetur, from",
        "field2": "more recently with desktop",
        "field3": "Contrary to popular belief"
    },
    "week03": {
        "date": {
            "$date": "2021-04-21T12:00:00.000Z"
        },
        "field1": "going through",
        "field2": "readable English",
        "field3": "Contrary to popular belief"
    },
    "week04": {
        "date": {
            "$date": "2021-04-28T12:00:00.000Z"
        },
        "field1": "words which don't look",
        "field2": "sure there isn't",
        "field3": "Contrary to popular belief"
    },
    "week05": {
        "date": {
            "$date": "2021-05-05T12:00:00.000Z"
        },
        "field1": "only five centuries",
        "field2": "Where can I get some",
        "field3": "infancy. Various"
    }
}

const weeks = Object.entries(document) // will be an array of key/value pairs
  .filter(([key, value]) => key.includes('week')) // only use week fields
  .map(([key, value]) => value) // only use the value

Template.helpers({
  allWeeks () {
    return weeks
  }
})

然后您可以通过 {{#each week in allWeeks}}.

对其进行迭代

注意:这有点低效,因为在每次重绘时它都会 运行 上面的代码。您将引入 ReactiveVar 或 ReactiveDict 并在其中保存 week 和 return 来自此反应源的值:

const state = new ReactiveDict()
const weeks = Object.entries(document) // will be an array of key/value pairs
  .filter(([key, value]) => key.includes('week')) // only use week fields
  .map(([key, value]) => value) // only use the value

state.set({ weeks })


Template.helpers({
  allWeeks () {
    return state.get('weeks')
  }
})

读数:

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/entries

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map

https://docs.meteor.com/api/reactive-dict.html