node.js - 停止回调链并在 Save 方法 ApostropeCMS 之前发送通知

标签 node.js apostrophe-cms

我试图阻止用户保存未达到某些要求的作品。

目前我正在这样做:

    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 .

知道如何停止保存项目并仅显示我的通知吗?

提前致谢。

更新1: 正如汤姆指出的,我的代码现在看起来像这样:

// lib/modules/events/public/js/editor-modal.js
apos.define('events-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' });
      }
    };
  }
});
// lib/modules/events/index.js
    var superPushAssets = self.pushAssets;
    self.pushAssets = function() {
      superPushAssets();
      self.pushAsset("script", "editor-modal", { when: "user" });
    };
    self.beforeSave = async function(req, piece, options, callback) {
      return callback("incompatible")
    };

出于测试目的,我只是在 beforeSave 中返回错误。问题是浏览器控制台中抛出异常,并且模式未再次正确呈现。这是我正在谈论的内容的屏幕截图: enter image description here

我正在尝试调试它并了解发生了什么,但还没有任何线索。

最佳答案

在服务器端代码中:

    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') {
        return 'A message suitable for this case.';
      } else {
        return 'A generic error message.';
      }
    };
  }
});

如果回调报告的错误是字符串,则将其传递给浏览器。然后浏览器可以识别这种情况并进行特殊处理。 'my-pieces-module-editor-modal' 应替换为您的pieces模块的名称,后跟-editor-modal

关于node.js - 停止回调链并在 Save 方法 ApostropeCMS 之前发送通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58181905/

相关文章:

javascript - 为什么 plyr 没有在任何浏览器中以全屏模式全屏播放视频

javascript - 使用 `target: node` 时如何说服 webpack 使用 Node 导入而不是 web 导入

npm - 永远安装 npm 和 mechanic 时出错

node.js - 撇号 cms 中图像的默认值?

node.js - 开发 Alexa Skill 时如何测试高级交互

javascript - 发出 http.request 返回 undefined

apostrophe-cms - 在 Apostrope CMS 中自定义件管理模式

javascript - ApostropeCMS - 在快速 route 渲染完整的 HTML 页面

apostrophe-cms - 如何通过电子邮件发送 ApostropheCMS 联系表格提交?

node.js - 使用 mongoose 显示来自 mongodb 的图像