1. #1
    Sencha User
    Join Date
    Mar 2011
    Posts
    150
    Answers
    5
    Vote Rating
    4
    incutonez is on a distinguished road

      0  

    Default Answered: Constructor... should I always use this.callParent(arguments)?

    Answered: Constructor... should I always use this.callParent(arguments)?


    In every constructor, should I be using this.callParent(arguments) or this.callParent()? Depending on which one, is it best to call it at the beginning, end, or whenever you need it (eg, you want to use the add method of the tbar, so you have to call callParent before)? I only ask because when searching in the API, "constructor" turns up 3 results, and all of them aren't relevant.

    Also semi-related, when creating a component, it appears I can't pass in a model as a config object, as a model contains mixins, which messes up somewhere in the this.callParent(arguments) call, so I'm assuming I have to pass in an actual object with the model as a value to a key... small example:

    Code:
    Ext.define('MyModel', {
      extend: 'Ext.data.Model',
      fields: [{
        name: 'firstName', type: 'string'
      }]
    });
    
    Ext.define('MyWindow', {
      extend: 'Ext.window.Window',
      height: 300,
      width: 300,
      autoShow: true,
      title: 'Test Window',
      constructor: function(config) {
        // use config
        this.callParent(arguments);
      }
    });
    
    var model = Ext.create('MyModel');
    var window = Ext.create('MyWindow', {model: model});  // shows window
    window = Ext.create('MyWindow', model);  // TypeError: me.mixins.elementCt is undefined
    I'm just trying to figure out what the best practice is here... I'm assuming it's the former (passing in an object), but I'm also curious about my first question as well. Thanks!

  2. You should usually avoid overriding constructors except where there isn't a more suitable method. For components you'd override the template method initComponent instead. The initComponent method is called after the config has been 'applied' to the object:

    Code:
    Ext.apply(this, config);
    initComponent doesn't take any arguments so you'd just use this.callParent() instead of this.callParent(arguments). Config options will be accessible as properties on the this reference, overriding any that might be set on the prototype by the Ext.define calls of its class hierarchy.

    Calling callParent isn't specific to constructors, you'd do exactly the same for any method you override.

    If you try to pass a model directly to a window as a config it'll cause problems when it's applied, equivalent to:

    Code:
    Ext.apply(win, model);

  3. #2
    Touch Premium Member
    Join Date
    Nov 2010
    Location
    Chicago
    Posts
    1,340
    Answers
    13
    Vote Rating
    131
    LesJ is a name known to all LesJ is a name known to all LesJ is a name known to all LesJ is a name known to all LesJ is a name known to all LesJ is a name known to all

      2  

    Default


    You should have the this.callParent(arguments) or this.callParent([config]) call in the constructor.

    Placement of this call in the subclass matters. It can be the first or the last line of code in the constructor or even in the middle.

    It all depends when the superclass' constructor should be called.

  4. #3
    Sencha User
    Join Date
    Mar 2011
    Posts
    150
    Answers
    5
    Vote Rating
    4
    incutonez is on a distinguished road

      0  

    Default


    Ok, thanks for reassuring my assumptions. Is there documentation on the generic constructor somewhere? I realize each class has the new <ClassName> in the API, but that doesn't really tell you about the constructor itself (like needing this.callParent in the constructor).

  5. #4
    Sencha Premium Member skirtle's Avatar
    Join Date
    Oct 2010
    Location
    UK
    Posts
    3,605
    Answers
    543
    Vote Rating
    326
    skirtle has a brilliant future skirtle has a brilliant future skirtle has a brilliant future skirtle has a brilliant future skirtle has a brilliant future skirtle has a brilliant future skirtle has a brilliant future skirtle has a brilliant future skirtle has a brilliant future skirtle has a brilliant future skirtle has a brilliant future

      2  

    Default


    You should usually avoid overriding constructors except where there isn't a more suitable method. For components you'd override the template method initComponent instead. The initComponent method is called after the config has been 'applied' to the object:

    Code:
    Ext.apply(this, config);
    initComponent doesn't take any arguments so you'd just use this.callParent() instead of this.callParent(arguments). Config options will be accessible as properties on the this reference, overriding any that might be set on the prototype by the Ext.define calls of its class hierarchy.

    Calling callParent isn't specific to constructors, you'd do exactly the same for any method you override.

    If you try to pass a model directly to a window as a config it'll cause problems when it's applied, equivalent to:

    Code:
    Ext.apply(win, model);

  6. #5
    Sencha User
    Join Date
    Mar 2011
    Posts
    150
    Answers
    5
    Vote Rating
    4
    incutonez is on a distinguished road

      0  

    Default


    Well, the reason why I'm overriding the constructor is because I'm actually passing in a configuration object that gets handed down from a web service, so I need to process these configuration files.

    I believe you both have cleared up any issues I was having (except for passing in the model as a config object, but I think that param just has to be an object). Thanks!

Thread Participants: 2

Tags for this Thread