2015-09-25 74 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