Log in or register to post comments
Last post
freeride's picture
Joined: 13/03/2010
Posts: 6
destroy an editorInstance
Hi,

I have in my application to create aninctance of the eitor, after click on a button, show the editor in a new div, and destroy it after editing. It works fine. I used the sample of ajax.html form the install package as sample. But obviously the sample dosn't work fine. The instance will not be destroyed. the script will build for each click a new Instance of editor.
My proper problem is i have to config the editor, and this works only for the first click. after second click, the editor will be shown in the default configuration.

the original code from the ck-sample:
function createEditor()
{
   if ( editor )
      return;

   var html = document.getElementById( 'editorcontents' ).innerHTML;

   // Create a new editor inside the <div id="editor">
   editor = CKEDITOR.appendTo( 'editor' );
   editor.setData( html );
   editor.config.toolbar = [
    ['Bold', 'Italic', '-', 'NumberedList', 'BulletedList', '-', 'Link', 'Unlink', '-', 'Cut','Copy','Paste','-','Outdent','Indent','Blockquote', '-', 'Subscript','Superscript', '-', 'TextColor'],
    ['UIColor']
   ];

   // This sample may break here if the ckeditor_basic.js is used. In such case, the following code should be used instead:
   /*
   if ( editor.setData )
      editor.setData( html );
   else
   {
      CKEDITOR.on( 'loaded', function()
         {
            editor.setData( html );
         });
   }
   */
}

function removeEditor()
{
   if ( !editor )
      return;

   // Retrieve the editor contents. In an Ajax application, this data would be
   // sent to the server or used in any other way.
   document.getElementById( 'editorcontents' ).innerHTML = editor.getData();
   document.getElementById( 'contents' ).style.display = '';

   // Destroy the editor.
   editor.destroy();
   editor = null;
}


the part "ditor.config.toolbar ... " is done by me.
as i described above, the editor appears at first with the choosen buttons. all following appearances of the editor are in the full (default) version.

i hope you can help me to solve the problem.
jhotterb's picture
Joined: 16/03/2010
Posts: 5
Re: destroy an editorInstance
I had exactly the same problem. I didn't have a look at the ck-samples so I don't know about the editor.destroy but I had to call CKEDITOR.remove(<textarea-id>) after removing (or just hiding in my case) the div the editor so that it works the next time the div with the editor was displayed.

Jean
alfonsoml's picture
Joined: 31/12/2006
Posts: 3696
Re: destroy an editorInstance
@freeride:

Your problem is that you can't change the toolbar configuration after the instance has been created. The first time it takes slightly longer to create the editor as it has to load all the resources, but the next time it's done straight away so your editor.config.toolbar is useless.

@jhotterb

Please, DON'T use CKEDITOR.remove. You must call the destroy() method of the instance that you want to remove.
freeride's picture
Joined: 13/03/2010
Posts: 6
Re: destroy an editorInstance
Hi,

@alfonsoml at first thans for answer. actually the behavior you described sound like a bug. I should be able to define the property of the each instance of the object i create.

How ever I solve my problem by using the jQuery Adapter as folow:

function destroyEditor() {
   CKEDITOR.instances.editor1.destroy();
}
   
function createEditor() {

   $('#editor1').ckeditor(config);
}

it works very fine. The memory using increase a little bit, by each creating of a new instance, but it isn't dramatic.
alfonsoml's picture
Joined: 31/12/2006
Posts: 3696
Re: destroy an editorInstance
freeride wrote:Hi,

@alfonsoml at first thans for answer. actually the behavior you described sound like a bug. I should be able to define the property of the each instance of the object i create.

And you can, but you must do it in the proper way. Once the toolbar is created, any change to the config object won't be reflected as it's a static property (and as long as IE6 and IE7 are supported it will remain that way).
Instead you must call
CKEDITOR.appendTo( 'editor' , config)


http://docs.cksource.com/ckeditor_api/s ... #.appendTo
freeride's picture
Joined: 13/03/2010
Posts: 6
Re: destroy an editorInstance
Obviously you're right. but when you see the my source from the first post and this one from the sample ajax.html. you'll see the activity in this way:
- define config
- create editor with this config
- the editor will appear in my configuration
- destroy the editor
- click for a new editor
- define the config
- create editor with this config
- the editor will appear in full configuration

but anyway with jQuery it works fine
alfonsoml's picture
Joined: 31/12/2006
Posts: 3696
Re: destroy an editorInstance
freeride wrote:Obviously you're right. but when you see the my source from the first post and this one from the sample ajax.html. you'll see the activity in this way:
- define config
- create editor with this config
- the editor will appear in my configuration
- destroy the editor
- click for a new editor
- define the config
- create editor with this config
- the editor will appear in full configuration

but anyway with jQuery it works fine

I don't understand if you are asking something, stating some problem or just adding a comment because I don't need any need to use jQuery for this simple task
freeride's picture
Joined: 13/03/2010
Posts: 6
Re: destroy an editorInstance
at first i asked, becouse it is my first prject with CKEdit. And in myOpinion the sample script "ajax.html" in the install package, dosn't work in proper way. While i was waiting for an answer here in forum, i got the solution by myself. Becouse i found here some releted question, i would show my my way to work with.
if they are a better way, it would be great if you could modify the sample "ajax.html" in this way, that the editor will be created with modifyed config.