【问题标题】:Extjs: override specific listenersExtjs:覆盖特定的侦听器
【发布时间】:2014-07-10 07:52:43
【问题描述】:

我有一个窗口组件,我正在扩展它以创建不同的窗口。现在,close()hide() 侦听器函数完全一样,但 afterrender() 随每个实例而变化。

所以我有类似的东西:

Ext.define('abc.xyz.BaseWindow', {
    extend : "Ext.Window",
    listeners: {
        hide: function(el, eOpts){
            console.log('hide');
        },
        close: function(el, eOpts){
            console.log('close');
        }   
    }
});

还有:

Ext.define('abc.xyz.MyWindow', {
        extend : "abc.xyz.BaseWindow",
        listeners: {
            afterrender: function(el, eOpts){
                console.log('afterrender');
            }
        }
    });

但是,整个 listeners 对象被覆盖,并且永远不会调用 hide()close()。除了在每个扩展窗口中指定hide()close() 之外,有什么办法可以解决这个问题?

【问题讨论】:

    标签: extjs extjs4.2


    【解决方案1】:

    您可以在窗口中定义函数,调用它们并在窗口中覆盖它们,如下所示:

    Ext.define('abc.xyz.BaseWindow', {
        extend : "Ext.Window",
        onHide: function(){
            console.log('hide');
        },
        onShow: function(el, eOpts){
            console.log('close');
        },
        onAfterRender: function(el, eOpts){
            console.log('first after render');
        },
    
        initComponent: function () {
            var me = this;
    
            Ext.applyIf(me, {
                listeners: {
                    hide: me.onHide,
                    show: me.onShow
                    afterrender: me.onAfterRender
                }
            });
    
            me.callParent(arguments);
        }
    });
    

    还有:

    Ext.define('abc.xyz.MyWindow', {
        extend : "abc.xyz.BaseWindow",
        onAfterRender: function(el, eOpts){
            console.log('second after render');
        }
    });
    

    或者,如果您在基类中没有后渲染,则只需添加一个带有 (on) 的侦听器,例如 Evan Trimboli sais

    Ext.define('abc.xyz.MyWindow', {
        extend : "abc.xyz.BaseWindow",
        initComponent: function () {
            var me = this;
            me.callParent(arguments);
    
            me.on('afterrender', function(el, eOpts){
                console.log('second after render');
            });
        }
    });
    

    【讨论】:

    • 在这种情况下是的。但是,如果您在基类中有一个,则两个处理程序都将被调用.. 取决于您想要什么:覆盖或添加额外的侦听器:)
    • 感谢您的想法。我最终从基础中的侦听器配置中调用onAfterRender() 并在扩展窗口中指定函数。有用。不需要 initComponent 或 applyIf
    猜你喜欢
    • 2014-08-17
    • 2013-01-13
    • 2018-08-29
    • 2017-03-25
    • 1970-01-01
    • 1970-01-01
    • 2014-03-29
    • 2020-09-24
    • 1970-01-01
    相关资源
    最近更新 更多