Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you unbind an event from CKEditor?

to bind something to an onChange event, one would write something similar to this:

CKEDITOR.on( 'currentInstance', function( ev )
{
       if (CKEDITOR.instances[element_id].checkDirty()) {
          unsaved_changes = true;
       }
});

But... how does one unbind that that function?

The above code is part of some instantiating code that I use when creating an editor. An issue arises when I use ajax to change the page, and the CKEditor (and all other javascript variables) remain defined on the page. So, the onChange event ends up getting multiple bindings... which can cause performance issues.

like image 710
NullVoxPopuli Avatar asked Apr 30 '12 16:04

NullVoxPopuli


2 Answers

The eventInfo documentation of CKEditor is missing the "removeListener" method that you can find using Firebug. I've added it now but it might take a day until it's published.

You just have to call that method on the event object, for example:

CKEDITOR.on( 'currentInstance', function( ev )
{
       ev.removeListener();

       if (CKEDITOR.instances[element_id].checkDirty()) {
          unsaved_changes = true;
       }
});
like image 153
AlfonsoML Avatar answered Oct 17 '22 07:10

AlfonsoML


window.ckeditor=CKEDITOR.replace('ckeditor'); //create instance

var focus_action =function (){ console.log('focus'); }; /*callback must have own name*/

ckeditor.on('instanceReady',function (){ var _this=this;

   this.document.on('keyup',function (){
    console.log(ckeditor.checkDirty());
   });

   this.document.on('focus',focus_action); //bind our callback


   this.document.on('blur',function (){
     console.log('blur');
     _this.document.removeListener('focus',focus_action); //remove our callback
     //_this.document.removeAllListeners(); //remove all listeners
   });

});

like image 30
Roman Yudin Avatar answered Oct 17 '22 06:10

Roman Yudin