From c71b060dc21a8581a683c5971a2dd14736abd82a Mon Sep 17 00:00:00 2001 From: ansuz Date: Thu, 14 Jan 2016 05:14:45 -0500 Subject: [PATCH] main.js : wait until your editor is defined before trying to reference it. addresses a race condition that caused ckeditor to fail half the time. --- www/pad/main.js | 28 +++++++++++++++++++++------- 1 file changed, 21 insertions(+), 7 deletions(-) diff --git a/www/pad/main.js b/www/pad/main.js index 4a9a17673..3fc1f5edd 100644 --- a/www/pad/main.js +++ b/www/pad/main.js @@ -10,7 +10,7 @@ define([ var ifrw = $('#pad-iframe')[0].contentWindow; var Ckeditor = ifrw.CKEDITOR; - $(function () { + var andThen = function () { $(window).on('hashchange', function() { window.location.reload(); }); @@ -32,12 +32,26 @@ define([ ifrw.$('iframe')[0].contentDocument.body.innerHTML = Messages.initialState; var rtw = - RTWysiwyg.start(ifrw, - Config.websocketURL, - Crypto.rand64(8), - key.channel, - key.cryptKey); + RTWysiwyg.start(ifrw, // window + Config.websocketURL, // websocketUrl + Crypto.rand64(8), // userName + key.channel, // channel + key.cryptKey); // cryptKey editor.on('change', function () { rtw.onEvent(); }); }); - }); + window.editor = editor; + window.RTWysiwyg = RTWysiwyg; + }; + + var interval = 100; + var first = function () { + if (Ckeditor = ifrw.CKEDITOR) { + andThen(); + } else { + console.log("Ckeditor was not defined. Trying again in %sms",interval); + setTimeout(first, interval); + } + }; + + $(first); });