5

我有一个关于 ExtJS 中的多重继承的问题。虽然我知道我可以简单地复制代码以使其发生,但我想知道是否有任何方法可以更有效地对其进行编码。

GridPanel我的框架中有一个自定义组件,称为Kore.ux.grid.GridPanel. 它扩展Ext.GridPanel了额外的通用功能,并为 REST 操作提供了接口。

不久之后,我的同事希望也必须EditorGridPanel以相同的方式实现,即她希望它是可编辑的,同时具有轻松执行 REST 操作的能力。

我的问题是,有什么方法可以扩展Ext.EditorGridPanel来使用定制的Kore.ux.grid.GridPanel功能?

对于任何语法错误,我很抱歉,如果它令人困惑,我可以改写它。谢谢!!

编辑

我再次谷歌搜索,我发现线程说这是不可能的。如果我有这个问题,我应该遵循更好的编码模式吗?

谢谢!

再次编辑

很抱歉,我找到了适合我的结构。这是我发现对我有用的方法:

var Common = function(){}   //abstract class
Ext.apply(Common.prototype, {

    a : function(){},
    b: function(){}...

});

Ext.ux.SomePanelA = Ext.extend ( Ext.Panel, {

    stuff : ............

});

Ext.ux.SomePanelB = Ext.extend ( Ext.Panel, {

    diffStuff : ............

});

Ext.applyIf(Ext.ux.SomePanelA.prototype, Common.prototype);
Ext.apply(Ext.ux.SomePanelB.prototype, Common.prototype);

代码来源:http ://www.sencha.com/forum/showthread.php?48000-multiple-inheritance&p=228271&viewfull=1#post228271

如果您认为自己有更好的解决方案,请再次提供有用的建议:) 谢谢!

4

3 回答 3

5

你真正需要研究的是 ExtJS 插件。

/**
 * Boilerplate code taken from 'ExtJS in Action' by Jay Garcia
 */
YourNameSpace.RestGridPlugin = Ext.extend(Object, {
  constructor : function(config) {
    config = config || {};
    Ext.apply(this.config);
  },

  init : function(parent) { //parent argument here, is whatever you apply your plugin to
    parent.on('destroy', this.onDestroy, this);
    Ext.apply(parent, this.parentOverrides);
  },

  onDestroy : function() {
    //here you need to free any resources created by this plugin
  },

  parentOverrides : {
    //here you do your magic (add functionality to GridPanel)
  }

});

Ext.preg('restgridplugin',YourNameSpace.RestGridPlugin); //register your brand ne plugin

用法

someGrid = {
  xtype: 'grid',
  columns: ...
  store: ...
  plugins: [
    'restgridplugin'
  ]
}

someEditorGrid = {
  xtype: 'editorgrid',
  columns: ...
  store: ...
  plugins: [
    'restgridplugin'
  ]
}
于 2011-02-08T08:43:34.280 回答
0

创建Kore.ux.grid.AbstractGridPanel为具有一般功能的基类。并创建两个子类:Kore.ux.grid.GridPanelKore.ux.grid.EditorGridPanel(具有特定功能)。

于 2011-02-08T08:18:57.030 回答
0

我不同意在 Ext 中使用 Plugins 来完成多重继承。插件旨在改变行为或添加新功能。

实现多重继承的正确方法是使用 Mixins,请看这个惊人的解释SenchaCon 2011: The Sencha Class System

 Ext.define('FunctionalityA', {
     methodA: function () {
         ...
     }
 });

 Ext.define('FunctionalityB', {
     methodB: function () {

     }
 });

 Ext.define('MultipleFunctionality', {
     mixins: [
         'FunctionalityA',
         'FunctionalityB'
     ],

     method: function () {
         methodA();
         methodB();
     }
 });
于 2015-07-05T02:17:23.223 回答