Anh Tú
Anh Tú

Reputation: 636

How to remove event listeners while destroying CKEditor

I have a ckeditor plugin like this:

CKEDITOR.plugins.add('testplugin', {
        init: function (editor) {
               editor.on('contentDom', function (e) {                
                    var body = editor.document.getBody();           
                    body.on('mouseup', function (e) {
                       alert('run!!!!');
                     });
                  });
        });
});

It work perfect in CKEDITOR version 3 (iframe base)

But when i upgrade to CKEDITOR version 4 (lastest - contenteditable base),

all event fire multiple times when i destroy then re-init ckeditor.

(using CKEDITOR.instants.testEditor.destroy() and CKEDITOR.replace('testEditor',options);)

i use: removeAllListeners( ) to remove all event listeners to body but no change.

How can i complete destroy CKEDITOR 4 + All event listeners on it?

Upvotes: 2

Views: 3313

Answers (1)

oleq
oleq

Reputation: 15895

It seems that you use an inline instance, so editor.document is CKEDITOR.document. It means that every single instance shares the same var body = editor.document.getBody();.

To avoid duplicated events as a leftover of attached by "dead editors", you should either listen on editor#destroy and call event#removeListener on every single one, or use editable#attachListener which automates that job for you (jsFiddle):

editor.on( 'contentDom', function() {                                
    var body = editor.document.getBody();

    // This listener will be deactivated once editor dies.
    editor.editable().attachListener( body, 'mouseup', function() {
        console.log( 'run!!!!' );
    } );
} );

Upvotes: 4

Related Questions