当用户 saves/commits 时如何 add/update 片段字段

How to add/update piece field when the user saves/commits

在 ApostropheCMS 应用程序中,我们有一个片段类型“书”。前端用户可以从 CMS 更新文章 index.js.

中声明的字段

我们需要在用户保存后动态添加或更新一个字段,即citations字段。我们使用 Citation.js 根据编辑在 CMS 中输入的内容生成 MLA、Chicago 等引文。

我们不想让此字段在 CMS 中可见,因为它需要始终被 Citation.js 生成的结果覆盖。 (如果有办法添加一个字段并从 CMS 中隐藏它,那将是一个很好的解决方案!)。

我们目前的想法是在保存时添加字段(如果缺少)或更新它(如果存在):

(mostly) pseudo code
self.on('apostrophe-docs:afterSave', 'updateBook', async (req) => {
  const { piece } = req;

  // fetch citations
  const citations = { ... };

  // create update piece
  const updated = _.cloneDeep(piece);
  updated.citations = citations;

  // check if citations field already present
  if (!('citations' in piece)) {
    // add citations field

    // should method be different if field doesnt exist yet?
    self.update(req, updated);

  } else {
    // check when citations were last updated to ensure enough time diff to update

    // update citations field if all is well
    self.update(req, updated);
  }
});

正如预期的那样,这当前创建了一个无限循环,因为 'apostrophe-docs:afterSave' 在调用 self.update 之后被调用。

欢迎就如何实现这一目标提出任何建议。

beforeSave 更有可能是您应该使用的。如果您只是在将信息实际保存到数据库之前将信息添加到片段中,则无需调用 .update()

对于您关于可见性的问题,您无需将文档属性添加到片段架构中即可保存它们。字段需要位于要在 UI 中编辑或查看的片段架构中(即使设置为 readOnly: true)。

因此,在构建步骤中,您可以添加如下内容:

self.on('books:beforeSave', 'populateCitation');

self.populateCitation = function (req, piece, options) {
  // Being extra safe here.
  if (piece.type !== 'book') {
    return;
  }
  // Let's pretend `bookInfo` 
  if (piece.bookInfo) {
    // `getCitation` would be a method using Citation.js
    const citationInfo = getCitation(piece.bookInfo);
    piece.citation = citationInfo;
  }
};

然后您可以阅读文档中的 citation 属性 代码和(我很确定)模板中的内容,如果它存在(确保检查它)在打印前的模板中)。