4

我有这个父类:假设它的语法正确(视图别名、商店和 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 的 refs 吗?

4

2 回答 2

5

正如您自己发现的那样,该refs属性没有特殊处理,因此,是的,它确实被覆盖了。

要扩充它而不是替换它,您必须在子类构造函数中执行它,如下例所示:

Ext.define('myChild', {
    extend: 'myParent'
    ,constructor: function() {
        this.refs = (this.refs || []).concat([{
            ref: 'myReference',
            selector: 'myProduct'
        }]);
        this.callParent(arguments);
    }
});
于 2013-06-03T10:38:06.447 回答
2

这真的让我对 Ext 控制器感到烦恼,所以今天我尝试修复它,rixo 解决方案效果很好,但这是扩展每个子类的替代方法。扩展 Ext.app.Controller 而不是使用“onClassExtended”方法

    /**
 * 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);
        };
    }
});
于 2014-11-27T17:40:44.333 回答