【问题标题】:Set Keystone List "noedit" based on field value?根据字段值设置 Keystone 列表“noedit”?
【发布时间】:2017-07-06 09:58:00
【问题描述】:

我想让我的 Keystone List 对象仅在对象未发布时才可编辑。这是我简化的列表定义:

var Campaign = new keystone.List('Campaign', {
  nodelete: true,
  track: {
    createdAt: true,
  },
});

Campaign.add({
  ...,
  publish: {
    type: Types.Boolean,
    required: false,
    initial: false,
    dependsOn: {
      publishedOn: '',
    },
  },
  publishedOn: {
    type: Types.Datetime,
    label: 'Published On',
    hidden: true,
  },
});

仅当publishedOn 不为空时,是否可以设置noedit?我正在尝试防止对象在“发布”后被修改,并且缺少示例。

【问题讨论】:

    标签: keystonejs


    【解决方案1】:

    列表或字段的noedit 属性是模型定义的一部分,而不是单个项目定义的一部分。除非您想将其应用到整个字段或模型,否则无法在管理 UI 中将单个项目标记为不可编辑。

    如果您不太担心清晰度,而更担心确保无法编辑,您可以尝试以下方法:

    Campaign.schema.post('init', function () {
        if (this.published) this.invalidate('Published items cannot be edited');
    });
    

    如果您在项目发布后尝试保存项目,则会引发错误。

    虽然您可以使用dependsOn: { published: { $exists: true } } 过滤字段,但这会隐藏信息。

    【讨论】:

      【解决方案2】:

      有趣的是,我能够让publish 字段简单地检查自身:

      Campaign.add({
        ...,
        publish: {
          type: Types.Boolean,
          required: false,
          initial: false,
      
          dependsOn: {
            publish: false,
          },
      
        },
        ...,
      });
      

      现在它只显示publish 是否为false,这完全符合我的预期。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2014-05-15
        • 1970-01-01
        • 2012-06-27
        • 2022-11-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多