【问题标题】:cannot override mixin behavior after it's used in a class definition在类定义中使用后无法覆盖 mixin 行为
【发布时间】:2020-04-26 05:59:16
【问题描述】:

我需要重写一些由第 3 方库中的 mixin 添加的成员函数。问题是:mixin 立即在多个 3rd 方类定义中使用,在定义 mixin 的同一脚本文件中。而且我只能在此脚本之前或之后插入自定义代码,但不能在两者之间插入。如果我之后调用override,那么已经定义的类不会在调用链中获取我的函数。

// library script BEGIN
Ext.define('Foo.bar.Base', {
});


Ext.define('Foo.bar.Util', {
  newmember: function() {
    console.log('newmember');
  }
});


Ext.define('Foo.bar.Derived', {
   extend: 'Foo.bar.Base',

   mixins: {
       fooutil: 'Foo.bar.Util'
   }
});

// library script END

Foo.bar.Util.override({
   newmember: function () {
       console.log('newmember2');
       this.callParent();
   }
});


var obj = new Foo.bar.Derived();

obj.newmember();

实际输出:

newmember

期望的输出:

newmember2
newmember

【问题讨论】:

    标签: javascript extjs mixins


    【解决方案1】:

    在使用 mixin 定义类之前重写。这可以在定义时使用override 作为属性来完成:

    
    Ext.define('Foo.bar.UtilOverride',{
       override: 'Foo.bar.Util',
       newmember: function () {
           console.log('newmember2');
           this.callParent();
       }
    });
    
    // library script BEGIN
    Ext.define('Foo.bar.Base', {
    });
    
    
    Ext.define('Foo.bar.Util', {
      newmember: function() {
        console.log('newmember');
      }
    });
    
    
    Ext.define('Foo.bar.Derived', {
       extend: 'Foo.bar.Base',
    
       mixins: {
           fooutil: 'Foo.bar.Util'
       }
    });
    
    // library script END
    
    
    
    var obj = new Foo.bar.Derived();
    
    obj.newmember();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-07-02
      • 1970-01-01
      • 1970-01-01
      • 2016-06-04
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多