2014-09-02 21 views
0

聲明:我對ExtJS(版本5.01)相對較新。我希望能找到一些ExtJS專家來指出我的正確方向: 我在指定initComponent方法items配置時出現錯誤。下面的代碼生成錯誤:EXTJS項目配置中的內聯initComponent方法

「遺漏的類型錯誤:無法讀取的未定義的屬性‘項目’」

當北子面板的「initComponent」功能被註釋掉錯誤消失。我有這種感覺,我錯過了初始化順序。

Q:我如何可以指定一個子項items配置的initComponent方法?

Ext.define('MyApp.view.TestView', { 
    extend: 'Ext.panel.Panel', 
    title: 'Parent', 
    height: 300, 
    layout: 'border', 

    items: [{ 
     xtype: 'panel', 
     region: 'north', 
     title: 'North Child', 

     /* Problematic function: If commented, it works */ 
     initComponent: function(){ 
      console.log("test north child"); 
      this.callParent(arguments); 
     } 
    }], 


    initComponent: function(){ 

     console.log("Test parent"); 
     this.callParent(arguments); 
    } 
}); 
+0

你不能這樣做!看看[這個問題](http://stackoverflow.com/questions/14254321/best-practice-for-overriding-classes-properties-in-extjs/14254627#14254627)關於重寫的進一步信息。你的問題是,函數的範圍將是'MyApp.view.TestView'實例的範圍。 – sra 2014-09-02 05:58:04

+0

是否有一種ExtJS方法將上下文(即從配置生成的對象)附加到函數中?東西沿一個函數包裝與範圍配置的線? – user1362700 2014-09-02 06:17:57

+0

請告訴我們爲什麼您要在子項上使用initComponent函數。大多數時候,這是沒有必要的。 – Alexander 2014-09-02 07:16:11

回答

0

簡短的回答:你不能對孩子定義initComponent,因爲你不能做任何事情有不能在任何其他地方進行。

InitComponent在創建組件「MyApp.view.TestView」的實例時執行(您只在此處使用Ext.define定義它)。可以使用Ext.create('MyApp.view.TestView',{創建它,也可以創建另一個視圖,將該組件添加爲項目,或者派生另一個組件(extend:'MyApp.view.TestView')。

創建'MyApp.view.TestView'時也會創建所有子組件,因此子組件的initComponent函數將是多餘的,因爲無法在沒有父組件的情況下創建子組件,所以父組件的initComponent用於您想要在子項的initComponent中執行的所有操作。

如果你需要某物。要計算可以addded項目之前,你將進行如下:

Ext.define('MyApp.view.TestView', { 
    extend: 'Ext.panel.Panel', 
    title: 'Parent', 
    height: 300, 
    layout: 'border', 

    initComponent: function(){ 
     var me = this, 
      tf = Ext.getCmp("someTextField"), 
      myTitle = (tf?tf.getValue():''); 
     Ext.applyIf(me,{ 
      items: [{ 
       xtype: 'panel', 
       region: 'north', 
       title: myTitle, 
      }] 
     }); 
     this.callParent(arguments); 
    } 
}); 

請參考文檔究竟Ext.applyIf沒有(和它Ext.apply有什麼不同,因爲該功能也來得心應手有時)。

+0

謝謝。這有幫助!出於某種原因,我認爲子配置隱式包裝在匿名類定義中,類似於Ext.define()函數的配置對象。 – user1362700 2014-09-02 07:41:21