javascriptckeditor

CKEditor setData() function not working everytime


I am using CKEditor 4 on a textarea filed in a page, when I open data i nedit form a grid in my page, I put the setData() function to set the content inside the textarea inside a on instanceReady event, but sometimes it doesn't enter inside the instanceReady event function. This is my js code:

function prepCkEditor() {
  var oFCKeditor = CKEDITOR.instances.EMAIL_DESC_TEMPLATETextBox;
  if (!oFCKeditor) {
    CKEDITOR.replace('EMAIL_DESC_TEMPLATETextBox', {
        language: editorlang,
        toolbar: 'helpsi',
        skin: 'moono',
        scayt_autoStartup: CONFIGURAZIONE.enableSpellCheck,
        scayt_sLang: $('#hfUSER_LANGEXTENDED').val().replace('-', '_'),
        scayt_disableOptionsStorage: 'lang'
    });
  }
}

prepCkEditor();

var oFCKeditor = CKEDITOR.instances.EMAIL_DESC_TEMPLATETextBox;
if (oFCKeditor) {
   console.log(oFCKeditor.status);
   oFCKeditor.on('instanceReady', function () {
      oFCKeditor.setData(template.TemplateBody);
   });
}

The console.log statement put before the on instanceReady line prints 'ready' so it seems that the editor is fully loaded, but it doesn't enter inside the function to set the data. What could it be the problem?

If I check in debug in the browser development tools, it wiorks but the status that prints is unloaded


Solution

  • This is because sometimes it is ready and sometimes it is not by the time you reach this line of code.

    You should attach the event when you create the instance using the on property.

    function prepCkEditor() {
      var oFCKeditor = CKEDITOR.instances.EMAIL_DESC_TEMPLATETextBox;
      if (!oFCKeditor) {
        CKEDITOR.replace('EMAIL_DESC_TEMPLATETextBox', {
          language: editorlang,
          toolbar: 'helpsi',
          skin: 'moono',
          scayt_autoStartup: CONFIGURAZIONE.enableSpellCheck,
          scayt_sLang: $('#hfUSER_LANGEXTENDED').val().replace('-', '_'),
          scayt_disableOptionsStorage: 'lang',
          on: {
            instanceReady: function () {
              console.log(oFCKeditor.status);
              oFCKeditor.setData(template.TemplateBody);
            }
          }
        });
      }
    }