【问题标题】:are refs overriden or inherited in extjs?refs 在 extjs 中是否被覆盖或继承?
【发布时间】:2013-06-03 10:25:39
【问题描述】:

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

【问题讨论】:

    标签: javascript extjs extjs4 extjs-mvc extjs4.2


    【解决方案1】:

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

    要增强而不是替换,您必须在子类构造函数中进行,如下例所示:

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

    【讨论】:

      【解决方案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);
              };
          }
      });
      

      【讨论】:

        猜你喜欢
        • 2012-07-28
        • 1970-01-01
        • 2019-06-29
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-12-10
        • 1970-01-01
        相关资源
        最近更新 更多