KeystoneJS关系类型,按字段值限制可用项目

cod*_*gar 7 model-view-controller node.js keystonejs

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

基本上,模型有两组数组字段,而不是让管理员用户从字段中选择任何项目,我想仅限于属于特定集合_id的项目.

Jed*_*son 7

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

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

例如:

用户架构

User.add({
    state: { type: Types.Select, options: 'enabled, disabled' }
});
Run Code Online (Sandbox Code Playgroud)

发布架构

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

或者对于动态示例,假设您有role两个Posts和的设置Users.你只想匹配谁拥有相同的作者rolepost.

用户架构

User.add({
    userRole: { type: Types.Select, options: 'frontEnd, backEnd' }
});
Run Code Online (Sandbox Code Playgroud)

发布架构

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' } }
});
Run Code Online (Sandbox Code Playgroud)

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