2012-11-30 67 views
4

我在initComponent()中創建了一些項目 問題是,this.items以某種方式反駁了類變量,而不是實例變量。在initComponent中修改項目()

所以當我做兩個實例時,我最終得到兩個按鈕。

items: [], 
initComponent: function() { 
    this.items.push({ xtype: 'button', ... }) ; 
    this.callParent(arguments); 
} 

因爲我必須用推,每一次新的元素,會推入。

有一些實例相當於this.items當按鈕被創建之前,我可以修改定義或做我必須檢查手動複製?

回答

6

你不應該return this.callParent(arguments);

只是這就夠了:

initComponent: function() { 
    var me = this; 
    me.items = { xtype: 'button', ... }; //Are you sure items is filled up here? 
    me.callParent(); 
} 

此外,如果你正在寫您自己的'組件',並且您想在Ext.create中傳遞參數我總是執行以下操作:

constructor: function (config) { 
    var me = this; 
    Ext.apply(me, config); 
    me.callParent(); 
} 

這將覆蓋你的項目申報與一個你的手在Ext.create()

+0

arrr類。可能是'Ext.menu.Menu'希望項目是一個數組。如果我使用對象,則不會創建項目。如果我使用數組並將它們推入,它們會被創建,但是每次initComponent被調用時,都會被更多的推入,這就是爲什麼我最終在第一個地方遇到了這個問題:(在我的問題 –

+0

中編輯過的代碼initComponent只是被調用一次,但項目可以是數組或1對象(後來被轉換爲數組)。 –

1

你可以重載構造和調整在那裏的配置:

constructor: function(config) 
{ 
    config.items.someButton = { xtype: 'button', ... }; 
    this.callParent([config]); 
}