【问题标题】:CKEDITOR.replace() undefinedCKEDITOR.replace() 未定义
【发布时间】:2014-10-21 15:57:14
【问题描述】:

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

此代码返回: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;
    }

  });
});

不过,这个版本很适合:

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

  });
});

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

【问题讨论】:

  • 您是否尝试将其包装在 require(['ckeditor'], function(CKEDITOR){ 中?似乎其他插件正在干扰
  • 是的,正如我在第二个代码中提到的那样。但是这种方式'this'在require(..)中变得未定义;.

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


【解决方案1】:

您可以将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);
});

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-08-23
    • 2010-10-21
    • 2018-09-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多