我在Backbone View中有一个CKEditor实例,该插件带有一个插件,单击该插件即可将编辑器中的任何文本发布到我的网站上。我也想在单击时清除CKEditor,但使用setData不起作用。它闪烁,好像发生了什么,但随后重置为已经存在的相同数据。

在插件内部这样调用它:

注意:THIS是引用主干视图的变量

CKEDITOR.plugins.add( 'post', {
    init: function( editor ) {
        editor.addCommand('post', {
            exec: function(editor) {
                THIS.model.postMessageAttempt(editor.getData());
                THIS.options.data = "";
                editor.setData("");
            }
        });
        editor.ui.addButton('Post', {
            label: THIS.i18n.postText(),
            command: 'post'
        });

    }
} );


我也尝试过使用setData("some text")THIS.editor.setData("")(我对保存在View中的编辑器有引用),但是两者都具有相同的结果。关于出什么问题有什么想法吗?

编辑
我还尝试通过此调用THIS.$el.find('textarea' + this.id).val("");使用CKEditor与jQuery的集成,它不再闪烁,但仍不能清除编辑器。

最佳答案

问题是双重的:首先,setData在函数末尾调用函数afterSetData,而该函数调用getDatagetData在函数的开头调用函数beforeGetData,该函数调用setData。我认为目标是避开很多浅引用而不是深引用的事实,但是即使在未缩小的ckeditor.js中,也不清楚为什么这样做。

其次,我还调用disableEnablePost,以便在许多事件(焦点,key [down]等)上正确启用/禁用从编辑器到站点的发布。 disableEnablePost称为getData,这导致我在setData插件中调用post时出现计时问题。

问题:

CKEDITOR.plugins.add( 'post', {
    init: function( editor ) {
        editor.addCommand('post', {
            exec: function(editor) {
                THIS.model.postMessage(editor.getData());
                THIS.options.data = "";
                editor.setData("");
            }
        });
        editor.ui.addButton('Post', {
            label: THIS.i18n.postText(),
            command: 'post'
        });
        editor.on('key', function (event) {
            THIS.disableEnablePost(editor.getCommand( 'post' ), event);
        });
    }
} );


这是我的解决方案:

CKEDITOR.plugins.add( 'post', {
    init: function( editor ) {
        editor.addCommand('post', {
            exec: function(editor) {
                THIS.model.postMessage(editor.getData());
            }
        });
       ...
    }
} );


model.postMessage现在会在事件完成后触发一个事件,该事件会在视图中捕获,然后调用此函数:

clearRTE: function() {
    this.editor.setData("");
    this.options.data = "";
}


最后,我更改了disableEnablePost使其每次都不调用getData,但这不是一个好习惯。现在,它调用的editor.getSnapshot()更加轻巧,没有数据处理,也没有对getDatasetData的调用,因此对于频繁使用而言更好。

关于javascript - CKEditor setData无法清除或更改RTE中的文本,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/13385679/

10-09 23:47