Best Practices concerning initComponent() in Ext.define()

前端 未结 3 1994
广开言路
广开言路 2020-12-13 07:05

I\'m writing all my components in ExtJS\'s new MVC fashion using Ext.define().

I struggle a bit whether define properties inside of initComponent(

3条回答
  •  夕颜
    夕颜 (楼主)
    2020-12-13 07:28

    I want to add to Lionel's answer that it is better to declare any non-primitive config in initComponent. (By primitive I mean string, boolean and number). Array and Object go into initComponent.
    So definition should look like this:

    Ext.define('My.NewClass', {
      extend: 'OldClass',
      // here all primitive configs:
      cls: 'x-my-cls',
      collapsible: true,
      region: 'west',
      // and so on ...
    
      initComponent: function() {
        // here you declare non-primitive configs:
        this.tbar = [/* blah-blah */];
        this.columns = [/* blah-blah */];
        this.viewConfig = {/* blah-blah */};
        // and so on ...
    
        this.callParent(arguments);
      }
    
      // other stuff
    }
    

    The reason why you should put all non-primitive configs into initComponent is that otherwise configs of all instances will refer to the same objects. For example if you define NewClass like:

    Ext.define('My.NewClass', {
      extend: 'OldClass',
      bbar: Ext.create('Ext.toolbar.Toolbar', {
      // ...
    

    bbars of all instances will refer to the same object. And therefore every time you create new instance bbar disappears from the preveous instance.

提交回复
热议问题