7

是否可以通过指定值条件来限制 KeystoneJS 关系类型中可用的显示选项?

基本上,一个模型有两组数组字段,而不是让管理员用户从字段中选择任何项目,我只想限制为特定集合 _id 一部分的项目。

4

2 回答 2

7

不确定这是否正是您正在寻找的功能,但您可以filter在字段上指定一个选项Relationship作为对象,它将过滤结果,以便仅显示匹配的结果。

对象中的每个属性都filter应该是在相关架构中匹配的值,或者它可以是与架构中另一个值匹配的动态值path(您在路径前加上 a :)。

例如:

用户模式

User.add({
    state: { type: Types.Select, options: 'enabled, disabled' }
});

后架构

// Only allow enabled users to be selected as the author
Post.add({
    author: { type: Types.Relationship, ref: 'User', filter: { state: 'enabled' } }
});

或者对于一个动态示例,假设您对和都有一个role设置。您只想匹配与.PostsUsersrolepost

用户模式

User.add({
    userRole: { type: Types.Select, options: 'frontEnd, backEnd' }
});

后架构

Post.add({
    postRole: { type: Types.Select, options: 'frontEnd, backEnd' },
    // only allow users with the same role value as the post to be selected
    author: { type: Types.Relationship, ref: 'User', filter: { userRole: ':postRole' } }
});

请注意,这实际上并没有作为后端验证实现,它只是在管理 UI 中实现。所以它更多的是一种可用性增强而不是限制。

于 2014-05-28T14:29:13.993 回答
1

为了扩展 Jed 的答案,我认为正确的属性(至少在 KeystoneJS 0.2.22 的最新版本中)是“过滤器”而不是“过滤器”。“过滤器”对我不起作用。

于 2014-07-08T10:27:56.673 回答