根据字段值设置 Keystone 列表 "noedit"?

Set Keystone List "noedit" based on field value?

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

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吗?我试图防止对象在 "published" 之后被修改,并且缺少示例。

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

如果您不太担心清晰度,更担心无法编辑,您可以尝试以下操作:

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

如果您在项目发布后尝试保存它,这将导致抛出错误。

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

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

Campaign.add({
  ...,
  publish: {
    type: Types.Boolean,
    required: false,
    initial: false,

    dependsOn: {
      publish: false,
    },

  },
  ...,
});

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