【发布时间】:2020-11-16 23:57:36
【问题描述】:
在 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);
}
});
正如预期的那样,这当前创建了一个无限循环,因为在调用 self.update 之后调用了 'apostrophe-docs:afterSave'。
- 有没有办法传递参数来防止回调?
- 否则,我们正在考虑检查最后一次
update()发生的时间,有更好的建议吗?
- 否则,我们正在考虑检查最后一次
-
update()不添加传递给方法的片段上的字段吗?它只关心index.js中定义的字段吗?
欢迎就如何实现这一目标提出任何建议。
【问题讨论】: