2014-07-10 69 views
2

我有一个窗口组件,我正在扩展以创建不同的窗口。现在,close()hide()侦听器函数在主板上是相同的,但afterrender()随每个实例而变化。Extjs:覆盖特定听众

所以我有这样的:

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()之外,是否有解决此问题的方法?

回答

5

您可以在窗口中定义的功能,打电话给他们,在这样的窗口覆盖它们:

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

或者,如果你没有在一个AfterRender你只需添加一个监听器的基类,就像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'); 
     }); 
    } 
}); 
+3

你最好直接调用'on' t点。 –

+0

在这种情况下是的。但如果你在基类中有一个处理程序将被调用。取决于你想要的:覆盖或添加额外的监听器:) – VDP

+0

感谢您的想法。我最终调用'onAfterRender()'从基地的监听器配置并在扩展窗口中指定函数。有用。不需要initComponent或applyIf – Snowman