Jungle Coder
served by elixir, crafted via executive dysregulation
Just added post themes! Witness The Colors, and Exult!

CKEditor setData failing - Documenting the Arbitrary

UPDATE:

The lead CKEditor developer responded to this blog post via a ticket.

The post as it was before:

When programming, the most difficult parts are the arbitrary ones. These are things you don’t know and couldn’t reason your way with what you already know. When you are starting in programming, this is most things. “Simple” is only when you already know how it works. It gets better the longer you program, but “simple” still takes a lot of time.

A good example was a bug that I recently helped a colleague hunt down. Frank, my co-worker, was struggling to reproduce a bug related to the CKEditor library. Below is the code that was failing, and relevant documentation. The bug: Sometimes, but not very often, setting the data will fail to actually populate the editor with the html in data.

       gCurrentEditorObject.setData(data);

The fix happened to be twofold:


  if (CKEDITOR.status == 'loaded') {
      gCurrentEditorObject.setData(data);
  } else {
      CKEDITOR.on("instanceReady", function (event) {
          gCurrentEditorObject.setData(data);
      });
  }

The first part that Frank and I discovered was the code inside the else clause. Frank worked out the code if statement over the course of a couple hours the following day. The basic logic is “simple”. According to my coworker:

Pretty straightforward. There was a minor timing issue where in some cases if the object wasn’t ready, calling setData on the gCurrentEditorObject did absolutely nothing. Adding the wrapper to avoid any execution before instanceReady state prevented the setData call from occurring prematurely. To avoid breaking the timing of the instances where everything was in order, there is a simple check for status == 'loaded'. Simply put, if the editor object is loaded, then carry on, otherwise wait till it is loaded and THEN carry on.

I hope this helps anyone struggling with the lesser known parts of the CKEditor API. It does make me wonder why the editor code didn’t have this guard in place around setData already. Delaying the loading of a javascriptAPI isn’t unheard of, but it’d be nice for the function name to somehow communicate this. But, if you work with the CKEditor, this is just something you need to know. Heh.

Published January 26th, 2015

Comments

Previously: International Bible Day
Next: Academic Portfolio