我有这个父类:假设它的语法正确(查看别名,存储和xtypes):
Ext.define('myParent' {
extend : 'Ext.app.Controller',
refs : [{
ref : 'formwindow',
selector : 'forms-formwindow'
}, {
ref : 'mainForm',
selector : '#mainForm'
}],
});
i had this subclass :
Ext.define('myChild' {
extend : 'myParent',
});
每当我把这段代码放在我的子类中时:
refs : [{
ref : 'myReference',
selector : 'myProduct'
}],
我在运行时遇到了这个错误:
Uncaught TypeError: Object [object Object] has no method 'getMainForm'
我想知道来自父类的引用是否被我的子类重写....
发生了什么事?它真的覆盖了myParent的引用吗?答案 0 :(得分:5)
正如您自己发现的那样,对refs
属性没有特殊处理,所以,是的,它确实被覆盖了。
要扩充它而不是替换,你必须在子类构造函数中执行它,就像在这个例子中一样:
Ext.define('myChild', {
extend: 'myParent'
,constructor: function() {
this.refs = (this.refs || []).concat([{
ref: 'myReference',
selector: 'myProduct'
}]);
this.callParent(arguments);
}
});
答案 1 :(得分:2)
这真的让我对Ext控制器感到烦恼,所以今天我有一个解决它的问题,rixo解决方案效果很好但是这里可以扩展每个子类。使用“onClassExtended”方法
扩展Ext.app.Controller /**
* Extends Ext.app.Controller with the ability to have refs defined in
* Controllers and any of subclasses.
* Duplicate refs overwrite each other, last one in class hierarchy wins.
*/
Ext.define('App.Controller', {
extend: 'Ext.app.Controller',
//private make refs extensible by any subclasses
onClassExtended : function(cls, data, hooks) {
var onBeforeClassCreated = hooks.onBeforeCreated;
hooks.onBeforeCreated = function(cls, data) {
var me = this,
name = Ext.getClassName(cls),
prototype = cls.prototype,
superCls = cls.prototype.superclass,
thisRefs = data.refs || [],
superRefs = superCls.refs || [],
newRefs = [],
i = 0;
if (thisRefs.length > 0) {
for (i=0; i < thisRefs.length; i++) {
if (typeof thisRefs[i] !== 'undefined') {
newRefs.push(thisRefs[i]);
}
}
}
if (superRefs.length > 0) {
for (i=0; i < superRefs.length; i++) {
if (typeof superRefs[i] !== 'undefined') {
newRefs.push(superRefs[i]);
}
}
}
data.refs = newRefs;
onBeforeClassCreated.call(me, cls, data, hooks);
};
}
});