停止回调链并在保存方法ApostropheCMS之前发送通知
我试图阻止用户保存未满足某些要求的作品。
目前,我正在这样做:
self.beforeSave = function(req, piece, options, callback) {
let success = true;
let error = "";
if (Array.isArray(piece._subevents) && piece._subevents.length) {
success = self.checkDateAndTimeCompabilitiyWithChildren(piece);
}
if (!success) {
self.apos.notify(req, "Check the compatibility between parent event and subevents", { type: "error" });
error = "Subevents are not compatible with parent event";
}
callback(error);
};
这有效,但问题是它显示2条错误通知(默认和我的自定义),1条由于callback(error)
而1条由于apos.notify
。
知道如何停止保存项目并仅显示我的通知吗?
提前感谢。
回答如下:在服务器端代码中:
self.beforeSave = function(req, piece, options, callback) {
let success = true;
if (Array.isArray(piece._subevents) && piece._subevents.length) {
success = self.checkDateAndTimeCompabilitiyWithChildren(piece);
}
if (!success) {
return callback('incompatible');
}
return callback(null);
};
在浏览器端:
// in lib/modules/my-pieces-module/public/js/editor-modal.js
apos.define('my-pieces-module-editor-modal', {
extend: 'apostrophe-pieces-editor-modal',
construct: function(self, options) {
self.getErrorMessage = function(err) {
if (err === 'incompatible') {
apos.notify('A message suitable for this case.', { type: 'error' });
} else {
apos.notify('A generic error message.', { type: 'error' });
}
};
}
});
如果回调报告的错误是字符串,则会将其传递给浏览器。然后,浏览器可以识别这种情况并进行特殊处理。 'my-pieces-module-editor-modal'
应该替换为您的模块模块名称,后跟-editor-modal
。