2014-01-12 53 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

該方法仍然有點類似。 –