如何在模糊时销毁实例

CKEditor: HowTo destroy instance on blur?

本文关键字:实例 模糊      更新时间:2023-09-26

我有一个单一的html页面上有多个div元素。每次用户点击一个div,它被替换为一个CKEditor在飞行:

$('.editable').click(function() {
    editor = CKEDITOR.replace(this);
});

现在,如果CKEditor实例失去焦点(模糊事件),我需要通过ajax(如果发生变化)将内容发布到单独的脚本并销毁此实例:

$('.editable').click(function() {
    editor = CKEDITOR.replace(this);
    editor.on('blur', function(e)
    {
        if (e.editor.checkDirty())
           // get data with e.editor.getData() and do some ajax magic 
        e.editor.destroy();
    });
});

但是这个例子不会工作,因为,我不知道为什么,destory()将在checkDirty()之前被调用。我怎样才能使它工作?

如果将destroy()放在if()语句中呢?如果没有任何变化,可以使用else子句调用destroy。如果发生了更改,可以在数据传输完成后在If子句中调用destroy()

$('.editable').click(function() {
  editor = CKEDITOR.replace(this);
  editor.on('blur', function(e)
  {
    if (e.editor.checkDirty()) {
       // get data with e.editor.getData() and do some ajax magic
       if ( dataTransferComplete ) {
         e.editor.destroy();
       }
     } else {
       e.editor.destroy();
     }
  });
});

或者您可以在调用destroy()之前检查变量。在数据传输完成后,在else子句中将该变量设置为true,这样destroy()将不会被调用,直到您检查更改并传输任何更新的数据。

$('.editable').click(function() {
  editor = CKEDITOR.replace(this);
  editor.on('blur', function(e)
  {
    var okToDestroy = false;
    if (e.editor.checkDirty()) {
       // get data with e.editor.getData() and do some ajax magic
       okToDestroy = true;
     } else {
       okToDestroy = true;
     }
    if (okToDestroy )
      e.editor.destroy();
  });
});

这是一个大纲,我还没有测试代码,但它显示了概念。

Be Well,