2015-09-25 172 views
0

爲什麼下面的代碼不工作(ExtJS V6)?訪問類中的私有屬性

Ext.define('Test', { 
extend: 'Ext.window.Window', 
xtype: 'basic-window', 

config: { 
    mytitle: '' 
}, 

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

requires: [ 
      'Ext.form.Panel' 
     ], 

height: 300, 
width: 400, 
scope: this, 
title: 'title: ' + this.mytitle, 
autoScroll: true, 
autoShow: true, 
bodyPadding: 10, 
html: "Lorem ipsum", 
constrain: true, 
}); 

var t = Ext.create('Test', {mytitle: 'testtitle'}); 
t.show(); 

我認爲這會將窗口的標題設置爲「title:testtitle」。相反,它將標題設置爲「title:undefined」。

附加:如果我使用

... 
title: 'title' + this.getMytitle(), 
... 

我得到 「遺漏的類型錯誤:this.getMytitle不是一個函數」。爲什麼?

回答

3

第一個問題title: 'title: ' + this.mytitle評估,this沒有指向你的類的實例。你應該做的是從constructor

callParent呼叫預期的參數數組,它更容易隨時撥打this.callParent(arguments)

最後 只能調用this.getMytitle()你打過電話後,構造函數。

https://fiddle.sencha.com/#fiddle/uh9

constructor: function(config) { 
    this.callParent(arguments); 
    this.setTitle('title: ' + this.getMytitle())      
}, 

關於CONFIGS做設定

通過實施updateMytitle一個配置響應正確的方法,它還將每當有人呼籲setMytitle('title')

工作

https://fiddle.sencha.com/#fiddle/uha

Ext.define('Test', { 
    extend: 'Ext.window.Window', 
    xtype: 'basic-window', 
    requires: ['Ext.form.Panel'], 
    config: { 
     mytitle: '' 
    }, 

    updateMytitle: function(mytitle) { 
     this.setTitle('title: ' + mytitle);   
    }, 
+0

在這裏你可以找到更多有關神奇的getters,setters,更新和應用函數的信息:https://www.sencha.com/forum/showthread.php?171113-Difference-between-update-and- apply-magic-methods&p = 708489&viewfull = 1#post708489 – Tarabass

+0

@juan非常感謝!優秀的答案... – itsame69