【问题标题】:Ext.EventManager is deprecated in ExtJS 5ExtJS 5 中不推荐使用 Ext.EventManager
【发布时间】:2015-08-31 09:28:50
【问题描述】:

在 ExtJS 5 中,我收到控制台警告提示

[W] Ext.EventManager 已弃用。使用 Ext.dom.Element#addListener 来 附加一个事件监听器。

我正在使用下面的代码语句,

Ext.EventManager.on(window, 'beforeunload', function() {
    //code here
});

我知道 Ext.EventManager 已被弃用,但我应该如何替换我的代码语句以在 extjs 5 中使用它?

【问题讨论】:

    标签: javascript extjs extjs4 extjs5


    【解决方案1】:

    使用getWin() 方法并使用on 附加事件处理程序。

    Ext.getWin().on('beforeunload',function(){ 
       //code here
    });
    

    其他可能的选择是使用纯 JavaScript。

    window.onbeforeunload = function(){
        //Code here
    };  
    

    【讨论】:

    • 顺便说一句。 Ext.EventManager.on(document, 'keydown', ... 的替换为 Ext.getDoc().on('keydown', ...
    【解决方案2】:

    只要做:

    Ext.getWin().[m]on('beforeunload', function() {
        //code here
    });
    

    您可以在manual 中找到自己。形成文档:

    在 DOM 元素上注册事件处理程序。

    此类已弃用。请使用 Ext.dom.Element api 附加 DOM 元素的侦听器。例如:

    var element = Ext.get('myId');
    
    element.on('click', function(e) {
        // event handling logic here
    });
    

    【讨论】:

    • window.on('beforeunload', function() {} 抛出控制台错误Uncaught TypeError: window.on is not a function
    • window 是原生 DOM 元素; Ext.getWin() 返回 ExtJS 包装器。
    • 谢谢,罗伯特。编辑我的答案更准确:)
    猜你喜欢
    • 2019-08-18
    • 1970-01-01
    • 2012-02-09
    • 2018-03-17
    • 2020-09-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-22
    相关资源
    最近更新 更多