CKEDITOR.replace() undefined

4.8k Views Asked by At

I'm using backbone-forms and I want to replace my textArea with CKEDITOR.

This code returns : Uncaught TypeError: undefined is not a function (for CKEDITOR line)

define(['jquery', 'backbone', 'backbone-forms', 'ckeditor'],
  function($, Backbone, CKEDITOR) {

  var Form = Backbone.Form;
  Form.editors.Wysiwyg = Form.editors.TextArea.extend({
    className: 'form-control wysiwyg',

    render: function() {
      Form.editors.Base.prototype.render.call(this);
      this.setValue(this.value);

      CKEDITOR.replace(this.el.getAttribute('name'));
      /* At this point, in the consol I can just get:
      *  CKEDITOR.Editor ,.Field ...
      *  But not .replace, .remove ...
      */

      return this;
    }

  });
});

However, it plays nice with this verison:

define(['jquery', 'backbone', 'backbone-forms'],
  function($, Backbone) {

  var Form = Backbone.Form;
  Form.editors.Wysiwyg = Form.editors.TextArea.extend({
    className: 'form-control wysiwyg',

    render: function() {
      Form.editors.Base.prototype.render.call(this);
      this.setValue(this.value);
         require(['ckeditor'], function(CKEDITOR){
           CKEDITOR.replace("html_content"); //can't get this!
         });

      return this;
    }

  });
});

Any idea why the first code doesn't work ?

1

There are 1 best solutions below

0
Spokey On BEST ANSWER

You can save this to a variable and use it in another scope

render: function() {
  var t = this;
  Form.editors.Base.prototype.render.call(t);
  t.setValue(t.value);

  require(['ckeditor'], function(CKEDITOR){
     CKEDITOR.replace(t.el.getAttribute('name'));
  });

  return t;

}

or you can get the value first and then use it

var name = this.el.getAttribute('name');

require(['ckeditor'], function(CKEDITOR){
   CKEDITOR.replace(name);
});