CKEDITOR.replace()未定义

时间:2014-10-21 15:57:14

标签: javascript jquery backbone.js ckeditor backbone-forms

我使用骨干表单,我想用CKEDITOR替换textArea。

此代码返回:Uncaught TypeError:undefined不是函数(对于CKEDITOR行)

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;
    }

  });
});

但是,它对这个版本很有用:

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;
    }

  });
});

知道为什么第一个代码不起作用?

1 个答案:

答案 0 :(得分:2)

您可以将this保存到变量并在另一个范围内使用

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;

}

或者您可以先获取该值然后再使用

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

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