2013-01-22 14 views
1

我使用ExtJs 4.1和DeftJs。ExtJs - 構造函數重寫我的實例

一些類是有了這樣的構造函數中定義:

Ext.define('A.helper.Report', { 
    config: { 
    conf  : null, 
    viewMain : null 
    }, 
    constructor: function(oConfig) { 
    this.conf = oConfig.conf; 
    this.viewMain = oConfig.viewMain; 
    this.initConfig(oConfig); 
    } 
... 

現在,我創建這個類的幾個實例這樣的:

var class1 = Ext.create('A.helper.Report', { 
    conf: someValue, 
    viewMain: someObject 
}); 

var class2 = Ext.create('A.helper.Report', { 
    conf: otherValue, 
    viewMain: otherObject 
}); 

當使用這些情況下,雖然給他們不同oConfig數據,class1class2現在都有第二個oConfig的數據。

因此,在這兩種情況下調用this.conf時,我得到someValue

如何保存已創建實例的數據?


解決方案:

我寫

Ext.define('A.helper.Report', { 
    ... 
    references: {} 
    ... 

,並把我的情況下在那裏,覆蓋舊實例。

切換到references: null幫助。

...

回答

3

要小心,不要與原型對象亂搞......

改寫答案

你是做了錯誤的方式。

見的答案這個工作JSFiddle

Ext.define('A.helper.Report', { 
    config: { 
     conf  : null, 
     viewMain : null 
    }, 
    constructor: function(cfg) { 
     this.initConfig(cfg); 
    } 
}); 


var class1 = Ext.create('A.helper.Report',{ 
    conf: 66, 
    viewMain: 'Bear' 
}); 

var class2 = Ext.create('A.helper.Report',{ 
    conf: 88, 
    viewMain: 'Eagle' 
}); 

class1.getConf(); // 66 
class1.getViewMain(); // Bear 
class2.getConf(); // 88 
class2.getViewMain(); // Eagle 
+0

好,謝謝。我可以通過使用其他類概念解決這個問題嗎?你能舉一個例子/參考克隆和適用? – Patrick

+0

@Indianer看我的編輯。並介意腳註。 – sra

+0

嗨sra,嘗試了你的方法,但仍然是相同的數據。我也嘗試使用'initComponent()',但它在創建時根本不會被調用。 – Patrick