Extjs多重继承?

发布于 2024-10-16 12:34:54 字数 1309 浏览 3 评论 0原文

我有一个关于 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

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

I have a question regarding multiple inheritance in ExtJS. While I know I can simply duplicate the code to make it happens, but I want to know if there is any way to code it more efficiently.

I have a customized GridPanel component in my framework, called Kore.ux.grid.GridPanel. It extends Ext.GridPanel with extra common functionalities and provides interface for REST actions.

Soon later, my colleague wanted to have EditorGridPanel to be also implemented in the same manner, ie, she wants it to be editable, and at the same time, have the ability to do REST actions easily.

My question is, is there any way I can extend Ext.EditorGridPanel to take up the customized Kore.ux.grid.GridPanel functionalities?

Sorry for any grammar errors and I can rephrase it if it's confusing. Thanks!!

EDIT

I googled again, and I found threads saying it's impossible. Is there any better coding pattern I should follow if I have this problem?

Thanks!

EDIT AGAIN

So sorry I found the structure that suits me.. Here is the method I found useful to me:

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);

Code Source: http://www.sencha.com/forum/showthread.php?48000-multiple-inheritance&p=228271&viewfull=1#post228271

Please again provide useful suggestions if you think you have a better solution :) Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

旧时光的容颜 2024-10-23 12:34:54

你真正需要研究的是 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'
  ]
}

What you really need to look into, are ExtJS plugins.

/**
 * 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

Usage

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

someEditorGrid = {
  xtype: 'editorgrid',
  columns: ...
  store: ...
  plugins: [
    'restgridplugin'
  ]
}
乖乖兔^ω^ 2024-10-23 12:34:54

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

Create Kore.ux.grid.AbstractGridPanel as base class with general functionality. And create two child classes: Kore.ux.grid.GridPanel and Kore.ux.grid.EditorGridPanel (with specific functionality).

一身软味 2024-10-23 12:34:54

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

实现多重继承的正确方法是使用 Mixins,请观看这​​个精彩的解释SenchaCon 2011:Sencha 类系统

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

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

     }
 });

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

     method: function () {
         methodA();
         methodB();
     }
 });

I don't agree with using Plugins to accomplish multiple inheritance in Ext. Plugins are intended to changed behaviour or add new functionality.

The right way to achieve multiple inheritance is by using Mixins, please watch this amazing explanation 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();
     }
 });
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文