【问题标题】:KeystoneJS relationship type, limit available items by field valueKeystoneJS 关系类型,按字段值限制可用项
【发布时间】:2014-07-03 04:45:01
【问题描述】:

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

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

【问题讨论】:

  • 这个问题不够清楚。我不明白你想做什么,有什么问题。请进一步解释。并分享你已经拥有的代码来解决这个问题。

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


【解决方案1】:

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

【讨论】:

    【解决方案2】:

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

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

    例如:

    用户架构

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

    或者对于一个动态示例,假设您对PostsUsers 都有一个role 设置。您只想匹配rolepost 相同的作者。

    用户架构

    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 中实现。所以它更多的是增强可用性而不是限制。

    【讨论】:

      猜你喜欢
      • 2018-03-16
      • 2016-08-22
      • 2014-10-27
      • 1970-01-01
      • 2021-07-05
      • 2020-06-05
      • 2017-01-17
      • 1970-01-01
      • 2021-06-12
      相关资源
      最近更新 更多