keystonejs 模型中的动态类型 select

Dynamic type select in keystonejs model

我想在 adminUI 中使用组合框,其中包含来自网络服务的字段。我正在考虑使用 pre 'find' 挂钩获取数据,然后覆盖架构中 'audience' 属性 的选项属性。

架构:

Compliance.add({
  title: { type: Types.Text, required: true, initial: true, index: true }, 
  url: { type: Types.Url, required: true, initial: true },
  position: { type: Types.Number, initial: true },
  audience: { type: Types.Select, options: [], many: true, initial: true},
});

挂钩:

Compliance.schema.pre('find', async function(next) {
  let audiences = await audienceService.getAudiences();
  next();
})

但是我没有找到绑定数据的方法。知道如何做到这一点吗?

谢谢

您可以尝试从选项中创建一个函数:

function getAudiences() {
    return ['a', 'b', 'c'];
}

Compliance.add({
  title: { type: Types.Text, required: true, initial: true, index: true }, 
  url: { type: Types.Url, required: true, initial: true },
  position: { type: Types.Number, initial: true },
  audience: { type: Types.Select, many: true, initial: true, options: getAudiences() }
});

结果如下: