CKEDITOR.setData阻止使用.on函数附加事件 [英] CKEDITOR.setData prevents attaching of events with .on function

查看:761
本文介绍了CKEDITOR.setData阻止使用.on函数附加事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经建立了几个自定义插件,但只有一个是听键盘的关键事件。
在代码下面你可以看到设置事件的设置。 (和它有点基本)



现在我有以下问题,如果我设置我的数据与editor.setData在一个instanceReady listener.on函数不是设置。



我尝试用instanceReady事件替换contentDom,但是这也不能解决它。



如果我设置数据手动:editor.document.getBody()。setHtml(html),没有问题。并且所有事件都没有任何问题。

  CKEDITOR.plugins.add('myPlugin',{
lang :'',//%REMOVE_LINE_CORE%

init:function(editor){

//如果Dom准备好就绑定事件
editor.on 'contentDom',function()
{
// keydown
editor.document.on('keydown',function(e)
{

有没有人知道为什么会发生这种情况?setData函数只设置html还是重新加载编辑器还是什么? >

我已经看过这个 Ckeditor来源
但我认为这不是与setData函数有关的代码。



我不是要求解决方案。我想了解为什么会发生。

解决方案

编辑器#contentDom 每次设置新的内部文档时触发。在框架编辑器编辑器#setData()不仅替换 body.innerHTML 而是替换整个文档,因此

因此,您的代码在每个 setData()但你不删除旧的。由于不清楚的原因,这两个侦听器都不会在 keydown 上触发。我最近找到了这个,我不能解释这个事实。



无论如何,你需要分离所有侦听器 editor#contentDomUnload 。幸运的是,使用 editable#attachListener

  editor.on('contentDom',function(){
var editable = editor.editable();

editable.attachListener(editor.document,'keydown',function(){
...
});
});

监听器将在下一个 contentDomUnload


I have build a few custom plugins, but only one is listening to key events of the keyboard. Below in the code you can see the set-up to set the events. (and it's kinda basic)

Now i have the following problem that if i set my data with editor.setData in a instanceReady listener.that the .on functions aren't set.

I did try to replace the contentDom with the instanceReady event, but that doesn't fix it either.

if i set the data manualy with: editor.document.getBody().setHtml(html), there are no problems. and all events are attached without any problems..

CKEDITOR.plugins.add( 'myPlugin', {
    lang: '', // %REMOVE_LINE_CORE% 

    init: function( editor ) {

        //Bind events if the Dom is ready!
        editor.on( 'contentDom', function()
        {
                //keydown
                editor.document.on('keydown', function(e)
                {

Does anyone know why this is happening? Does the setData function only set the html or does it also reload the editor or something?

I did take a look at this Ckeditor Source But i think that this isn't code that has something to do with the setData function.

I'm not asking for a solution. I like to understand why it's happening.

解决方案

Editor#contentDom is fired every time new inner document is set up. In framed editor editor#setData() replaces not only body.innerHTML but entire document, so contentDom is fired every time.

Thus, your code adds new listener on every setData() but you don't remove the old one. For unclear reasons none of these two listeners are now fired on keydown. I found out this recently and I cannot explain this fact.

Anyway, you need to detach all listeners on editor#contentDomUnload. Fortunately, there's convenient way to do that by using editable#attachListener.

editor.on( 'contentDom', function() {
    var editable = editor.editable();

    editable.attachListener( editor.document, 'keydown', function() {
        ...
    } );
} );

Listener will be automatically detached on next contentDomUnload.

这篇关于CKEDITOR.setData阻止使用.on函数附加事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆