1. #1
    Sencha User
    Join Date
    Apr 2012
    Posts
    14
    Vote Rating
    0
    lotrmj is on a distinguished road

      0  

    Default MVC. Why do we use initComponent() method in View???

    MVC. Why do we use initComponent() method in View???


    Hi, look at these:
    Code:
    Ext.define('mv.view.some', {
        extend: 'Ext.Panel',
        alias: 'widget.some',
    
        title: 'Some',
    
        initComponent: function() {
            this.items = [
                {
                    xtype: 'form',
                    items: [
                        {
                            xtype: 'textfield',
                            name : 'name',
                            fieldLabel: 'Name'
                        },
                        {
                            xtype: 'textfield',
                            name : 'email',
                            fieldLabel: 'Email'
                        }
                    ]
                }
            ];
    
            this.callParent(arguments);
        }
    });
    Code:
    Ext.define('mv.view.some', {
        extend: 'Ext.Panel',
        alias: 'widget.some',
    
        title: 'Some',
    
            items: [
                {
                    xtype: 'form',
                    items: [
                        {
                            xtype: 'textfield',
                            name : 'name',
                            fieldLabel: 'Name'
                        },
                        {
                            xtype: 'textfield',
                            name : 'email',
                            fieldLabel: 'Email'
                        }
                    ]
                }
            ]
    });
    Result appears to be the same (at least visually). So why you use initComponent()? What is advantage of this?

  2. #2
    Ext JS Premium Member westy's Avatar
    Join Date
    Feb 2009
    Location
    Bath, UK
    Posts
    966
    Vote Rating
    72
    westy is just really nice westy is just really nice westy is just really nice westy is just really nice westy is just really nice

      0  

    Default


    Items defined inline are processed at class definition time, so you have no state, or config.

    You define your items in initComponent in case you want to perform some processing to determine what controls to include, exclude, or initialise them differently. Or perhaps to control the form panels content from the config object that was passed to the constructor.

    I can think of many reasons why you'd want to do this, and surely you can too!
    Product Architect
    Altus Ltd.

Thread Participants: 1