1. #1
    Ext User wceuppens's Avatar
    Join Date
    Dec 2009
    Posts
    54
    Vote Rating
    0
    wceuppens is on a distinguished road

      0  

    Default when to use initcomponent and when constructor?

    when to use initcomponent and when constructor?


    I had a problem recently while using initcomponent:
    Code:
    Ext.ns("APP.view");
    APP.view.North = Ext.extend(Ext.Toolbar, {
        initComponent : function() {
            Ext.apply( this, {
                region    : 'north',
                height    : 30,            // give north region a height
                border    : false,
                margins    : '0 5 0 5',
                items    : this.buildTbar()
            });
            APP.panel.North.superclass.initComponent.call(this);
        },
        .....
    using constructor solved the problem:
    Code:
    Ext.ns("APP.view");
    APP.view.North = Ext.extend(Ext.Toolbar, {
        constructor : function(config) {
    //  config = config || {};
        Ext.applyIf( config, {
            region    : 'north',
            height    : 30,            // give north region a height
            border    : false,
            margins    : '0 5 0 5',
            items    : this.buildTbar()
        });
        APP.view.North.superclass.constructor.call(this, config);
        },
        .....
    My question:
    Are there situations where it is preferable to use initcompontent? Or is it save to say that constructor is a more safe option in any case?
    Kind Regards,
    Willy. (Averell Dalton)

  2. #2
    Sencha - Ext JS Dev Team Animal's Avatar
    Join Date
    Mar 2007
    Location
    Notts/Redwood City
    Posts
    30,502
    Vote Rating
    47
    Animal has a spectacular aura about Animal has a spectacular aura about

      0  

    Default


    Because the instances of configs being accessed at construction time, or configs access from initialConfig are not documented, I would say always write your own constructor.

    Copy configs into a new object, and fiddle that before calling the superclass constructor.

  3. #3
    Sencha User
    Join Date
    Apr 2008
    Posts
    99
    Vote Rating
    0
    Bulle Bas is on a distinguished road

      0  

    Default


    I just asked that question on stackoverflow.

    I wonder why so many examples and books use initComponent. Does it have advantages over constructor?


    Copy configs into a new object, and fiddle that before calling the superclass constructor.
    I think wceuppens did that, or do you mean something differently? It is similar to the guidelines in the wiki.

  4. #4
    Sencha - Ext JS Dev Team Animal's Avatar
    Join Date
    Mar 2007
    Location
    Notts/Redwood City
    Posts
    30,502
    Vote Rating
    47
    Animal has a spectacular aura about Animal has a spectacular aura about

      0  

    Default


    No, wceuppens mutates the config object in his constructor. You have to be careful.

  5. #5
    Sencha - Ext JS Dev Team Animal's Avatar
    Join Date
    Mar 2007
    Location
    Notts/Redwood City
    Posts
    30,502
    Vote Rating
    47
    Animal has a spectacular aura about Animal has a spectacular aura about

      0  

    Default


    The code must be


    Code:
    config = Ext.apply({
        foo: 'default foo value',
        ...
    } config);
    You copy config INTO a brand new object, and then pass that NEW OBJECT into the superclass constructor for it to do its business with.

  6. #6
    Sencha User
    Join Date
    Apr 2008
    Posts
    99
    Vote Rating
    0
    Bulle Bas is on a distinguished road

      0  

    Default


    If the passed config has references itself, you are still not safe, are you? For example
    Code:
    var ref = new Array(1,2,3)
    var config = {myRef: ref};
    I see no cloning in Ext.apply:

    PHP Code:
    Ext.apply = function(ocdefaults){
        
    // no "this" reference for friendly out of scope calls
        
    if(defaults){
            
    Ext.apply(odefaults);
        }
        if(
    && && typeof c == 'object'){
            for(var 
    p in c){
                
    o[p] = c[p];
            }
        }
        return 
    o;
    }; 

    Otherwise, we need to update the wiki I think?

  7. #7
    Sencha - Ext JS Dev Team Animal's Avatar
    Join Date
    Mar 2007
    Location
    Notts/Redwood City
    Posts
    30,502
    Vote Rating
    47
    Animal has a spectacular aura about Animal has a spectacular aura about

      0  

    Default


    Your code creates a new object, and assigns config to reference it. It's fine.

  8. #8
    Sencha - Community Support Team VinylFox's Avatar
    Join Date
    Mar 2007
    Location
    Baltimore, MD
    Posts
    1,501
    Vote Rating
    8
    VinylFox will become famous soon enough VinylFox will become famous soon enough

      0  

    Default


    Did I miss what the original posters problem was?

    I use initComponent over constructor most of the time and can't say I have run into any problems yet.

  9. #9
    Sencha - Ext JS Dev Team Animal's Avatar
    Join Date
    Mar 2007
    Location
    Notts/Redwood City
    Posts
    30,502
    Vote Rating
    47
    Animal has a spectacular aura about Animal has a spectacular aura about

      0  

    Default


    But you probably set initialConfig properties?

  10. #10
    Sencha - Community Support Team VinylFox's Avatar
    Join Date
    Mar 2007
    Location
    Baltimore, MD
    Posts
    1,501
    Vote Rating
    8
    VinylFox will become famous soon enough VinylFox will become famous soon enough

      0  

    Default


    I guess I must be if im not running into problems.

    Honestly the OP's code seems silly to me, why would you hard code the region of the component your creating, which is where he must have run into trouble since that is set in the constructor. All of the other options he set are trivial.

Similar Threads

  1. Replies: 0
    Last Post: 1 Jul 2010, 4:50 AM
  2. constructor & initComponent usecases
    By nickar in forum Ext 2.x: Help & Discussion
    Replies: 3
    Last Post: 6 Jul 2009, 1:07 PM
  3. initComponent or constructor
    By nomack84 in forum Ext 2.x: Help & Discussion
    Replies: 3
    Last Post: 3 Apr 2009, 11:02 AM
  4. constructor Vs initComponent
    By santosh.rajan in forum Ext 2.x: Help & Discussion
    Replies: 27
    Last Post: 16 Sep 2008, 5:45 AM
  5. Confused about using initComponent or constructor
    By michael.piecko in forum Ext 2.x: Help & Discussion
    Replies: 4
    Last Post: 20 Apr 2008, 12:36 AM

Thread Participants: 6