0

我正在使用主干形式,我想用 CKEDITOR 替换我的 textArea。

此代码返回: Uncaught TypeError: undefined is not a function(对于 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;
    }

  });
});

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

4

1 回答 1

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);
});
于 2014-10-21T17:19:38.387 回答