【问题标题】:Meteor SimpleSchema Repeated Nested ObjectsMeteor SimpleSchema 重复嵌套对象
【发布时间】:2016-06-29 14:18:50
【问题描述】:

我想知道是否有一种简单的方法可以做到这一点。首先这是我当前的架构:

Test.schema = new SimpleSchema({
  owner: {
    type: String,
    label: 'Owner of the test',
  },
  name: {
    type: String,
    label: 'Name of the test',
  },
  createdAt: {
    type: Date,
    label: 'Date of test creation',
  },

  // MY QUESTION IS FOR THE OBJECT BELOW

  [LOVE]: {
    type: Object,
    label: `Configuration for ${LOVE}`,
  },
  [LOVE.one]: { type: Boolean },
  [LOVE.two]: { type: Boolean },
  [LOVE.three]: { type: Boolean },
  [LOVE.four]: { type: Boolean },
  [LOVE.five]: { type: Boolean },
  [LOVE.six]: { type: Boolean },
  [LOVE.seven]: { type: Boolean },
  [LOVE.eight]: { type: Boolean },
  [LOVE.nine]: { type: Boolean },
});

在我有 LOVE 变量的地方,我希望它等于多个值,这样我就不必一遍又一遍地编写相同的架构。

我以为我可以使用正则表达式之类的东西,但我不知道。有人可以帮忙吗?

【问题讨论】:

  • 你想用什么正则表达式来识别(一、二、三……a.s.o)?也许我错了,但为什么不使用 Love Objects 的集合呢?或者可能有一个预处理器可以根据模板插入,所以你不必一遍又一遍地编写......如果是这样......
  • 变量 LOVE 应该等于许多值,所以我可以只写一次。对于每个不同的 LOVE 值,键一、二、三等总是相同的。我的错,我希望这更清楚。

标签: javascript meteor simple-schema


【解决方案1】:

只需使用嵌套模式:

lovers = new SimpleSchema({
  one:   {type: boolean},
  two:   {type: boolean},
  three: {type: boolean},
  four:  {type: boolean},
  five:  {type: boolean},
  six:   {type: boolean},
  seven: {type: boolean},
  eight: {type: boolean},
  nine:  {type: boolean},
});

然后在您的原始架构中重用它:

Test.schema = new SimpleSchema({
  owner: {
    type: String,
    label: 'Owner of the test',
  },
  name: {
    type: String,
    label: 'Name of the test',
  },
  createdAt: {
    type: Date,
    label: 'Date of test creation',
  },
  LOVE:    { type: lovers },
  PASSION: { type: lovers },
  DESIRE:  { type: lovers },
  etc...
});

希望这就是你所追求的。

【讨论】:

【解决方案2】:

应该是这样

  relations : { type : [boolean] }

那么你就有了

 LOVE.relations 

作为 8 个条目的集合。无需将其限制为我假设的这 8 个。

您可能会使用不同的术语,可能是 affairs 或更好的匹配,但背后的想法只是按照集合的用途来使用它。

【讨论】:

    猜你喜欢
    • 2017-06-29
    • 2016-02-07
    • 2017-02-28
    • 2015-03-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-19
    • 2020-10-08
    相关资源
    最近更新 更多