2013-06-03 70 views
4

我有这个父类:承担其在正确的语法(意见别名,商店和xtypes):被ref覆盖或在extjs中继承?

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的参考吗?

回答

5

正如你已经发现你自己,refs属性没有特殊处理,所以,它确实被覆盖。

为了加强它,而不是替代,你必须在这个例子做在子类的构造函数,如:

Ext.define('myChild', { 
    extend: 'myParent' 
    ,constructor: function() { 
     this.refs = (this.refs || []).concat([{ 
      ref: 'myReference', 
      selector: 'myProduct' 
     }]); 
     this.callParent(arguments); 
    } 
}); 
2

这真让我心烦约分机控制器所以今天我有一个走了修复它,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); 
     }; 
    } 
});