开发者

initComponent vs constructor when defining an object

开发者 https://www.devze.com 2023-04-10 21:55 出处:网络
When should I be using initComponent compared to constructor ? I have been using initComponent to extend my objects, but looking at the docs for Ext.define is see them using constructor all over then

When should I be using initComponent compared to constructor ?

I have been using initComponent to extend my objects, but looking at the docs for Ext.define is see them using constructor all over then place. what is the difference?

compare:

Ext.define('My.app.PanelPart2', {
    exte开发者_StackOverflow中文版nd: 'My.app.Panel',
    constructor: function (config) {
        this.callSuper(arguments); // calls My.app.Panel's constructor
        //...
    }
});

to

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

I am aware of the fact that some components don't init ( Im looking at you Ext.data.Store ), which leads me towards leaning towards only over writing the constructor, as that should be universal.


constructor is OOP standard for initializing objects instances and is available in every ExtJS class (everything created by Ext.define).

initComponent is ExtJS extension for initializing components - classes that extends Ext.Component. It uses templated method pattern and enables some standard initialization steps before and after your custom component init. Something like this:

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

My best pratices are to use initComponent whenever I'm creating custom component and constructor only for generic classes.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号