javascript - Meteor SimpleSchema 重复嵌套对象

标签 javascript meteor simple-schema

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

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 变量的地方,我希望它等于多个值,这样我就不必一遍又一遍地编写相同的模式。

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

最佳答案

只需使用嵌套架构:

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

希望这就是您想要的。

关于javascript - Meteor SimpleSchema 重复嵌套对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38088256/

相关文章:

javascript - 在函数中调用缺少参数的构造函数

javascript - Jquery/JavaScript 字符串匹配

meteor - 如何统计 meteor 出版物的当前订阅数量

javascript - SimpleSchema 匹配除 null 之外的任何类型

mongodb - 在 Autoform 中使用对象作为选项

javascript - 不阻塞 UI 的 jQuery 移动弹出窗口

javascript - 单击第二个按钮,我希望第一个和第二个按钮都着色

javascript - 如何使用 Meteor 进行账户输入?

javascript - 如何使用相同的表格使用 quickForm 更新/插入记录?

javascript - 是否可以使用 Meteor 在客户端中列出 SimpleSchema 模式的 allowedValues?