2014-01-12 54 views
1

我搜索在谷歌,但没有发现这个问题的任何答案...ExtJS的4.2.1 - 工具栏和委托点击事件按钮

我有了内的多个按钮的工具栏。我怎么会去委托点击事件到每个工具栏里面的按钮,而无需编写类似:

{ 
    xtype: 'button', 
    text: 'Click me', 
    handler: function() { 
     // do something... 
    } 
} 

为每个按钮?

+0

是否使用MVC? –

+0

我不是。我的计划是让工具栏功能有点像TabPanel,所以我需要做的就是在不同的面板之间切换。 –

回答

3

按你的意见,我会做这样的事情:

Ext.define('MyContainer', { 
    extend: 'Ext.container.Container', 

    initComponent: function() { 
     var handler = this.handleButton; 
     this.defaultType = 'button'; 
     this.items = [{ 
      text: 'A', 
      key: 'itemA', 
      handler: handler 
     }, { 
      text: 'B', 
      key: 'itemB', 
      handler: handler 
     }, { 
      text: 'C', 
      key: 'itemC', 
      handler: handler 
     }, { 
      text: 'D', 
      key: 'itemD', 
      handler: handler 
     }]; 
     this.callParent(); 
    }, 

    handleButton: function(btn) { 
     console.log(btn.key); 
     // do something with key 
    } 
}); 

Ext.onReady(function() { 

    new MyContainer({ 
     renderTo: document.body 
    }); 

}); 
+0

我结束了使用MVC:\但无论如何。我认为这将在稍后派上用场。 –

+0

该方法仍然有点类似。 –