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

时间:2014-05-14 22:38:18

标签: node.js model-view-controller keystonejs

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

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

2 个答案:

答案 0 :(得分:6)

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

filter对象中的每个属性应该是相关模式中要匹配的值,或者它可以是与模式中另一个path的值匹配的动态值(前缀为路径使用:)。

例如:

用户架构

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

或者对于动态示例,假设您rolePosts都设置了Users。您只想匹配与role具有相同post的作者。

用户架构

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

请注意,这实际上并未实现为后端验证,只是在管理界面中实现。因此,它更多的是可用性增强而不是限制。

答案 1 :(得分:1)

为了扩展Jed的答案,我认为正确的属性(至少在最新版本的KeystoneJS 0.2.22中)是'过滤器'而不是过滤'。 '过滤器'不适合我。