node.js - 用户保存/提交时如何添加/更新片段字段
问题描述
在 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
.
- 有没有办法传递参数来防止回调?
- 否则,我们正在考虑检查最后一次
update()
发生的时间,有更好的建议吗?
- 否则,我们正在考虑检查最后一次
- 不
update()
添加传递给方法的片段上的字段吗?它只关心 ? 中定义的字段index.js
?
欢迎就如何实现这一目标提出任何建议。
解决方案
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
在代码中读取文档上的属性,并且(我很确定)也可以在模板中读取属性(如果存在)(确保在打印之前在模板中检查它)。
推荐阅读
- orm - 从 orm.xml Doctrine 生成 php 实体
- c - 如何将范围(0,4)中的空格添加到C中的字符串
- rust - Rust:无法将 Trait RangeBounds 制成对象
- ag-grid - Ag-grid 不支持日期时间过滤器
- javascript - 为什么我使用 Phaser 3 构建的游戏(并在 VPS 服务器上运行)在大多数设备上加载缓慢?
- java - 通过 Arrays.copyOf() 生成的二维数组副本中的更改反映在原始数组中
- java - Gradle.Properties 更改未随 Micronaut 项目加载
- compilation - 如何将我的 NVRTC 程序源与文件相关联?
- json - 使用 Ruby API 呈现 json 错误:我应该使用“错误”还是“消息”键?
- sql - TSQL SET 命令在 EXEC 语句中不起作用