【问题标题】:initComponent vs constructor when defining an object定义对象时的initComponent vs构造函数
【发布时间】:2011-10-05 03:33:05
【问题描述】:

constructor 相比,我应该什么时候使用initComponent

我一直在使用 initComponent 来扩展我的对象,但是查看 Ext.define 的文档会看到它们到处都使用构造函数。有什么区别?

比较:

Ext.define('My.app.PanelPart2', {
    extend: 'My.app.Panel',
    constructor: function (config) {
        this.callSuper(arguments); // calls My.app.Panel's constructor
        //...
    }
});

Ext.define('My.app.PanelPart2', {
    extend: 'My.app.Panel',
    initComponent: function (config) {
        Ext.apply(this, config);
        this.callParent(arguments);
    }
});

我知道有些组件没有初始化(我在看着你Ext.data.Store),这导致我倾向于只编写构造函数,因为这应该是通用的。

【问题讨论】:

    标签: extjs4 extjs


    【解决方案1】:

    constructor 是初始化对象实例的 OOP 标准,可用于每个 ExtJS 类(由Ext.define 创建的所有内容)。

    initComponent 是用于初始化组件的 ExtJS 扩展 - 扩展 Ext.Component 的类。它使用模板化方法模式,并在您的自定义组件初始化之前和之后启用一些标准初始化步骤。像这样的:

    Ext.Component.constructor() {
        - init events
        - apply config
        - create plugins
        - ...
        - your custom component initialization (initComponent)
        - ...
        - init plugins
        - etc.
    }
    

    我的最佳做法是在创建自定义组件时使用initComponent,而constructor 仅用于泛型类。

    【讨论】:

    • 你好,这个定义现在还成立吗?使用 Extjs 的第 3 版。它的定义还能用吗?我仍然可以通过一些链接让我学习 InitComponent 吗?感谢您的帮助。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-10-16
    • 1970-01-01
    • 2011-03-11
    • 1970-01-01
    • 1970-01-01
    • 2019-04-22
    • 1970-01-01
    相关资源
    最近更新 更多