1. #1
    Sencha User
    Join Date
    Mar 2014
    Posts
    12
    Answers
    1
    Vote Rating
    0
    FSolutionsDev is on a distinguished road

      0  

    Default Unanswered: Howto Override / Change the constructor signature in SA

    Unanswered: Howto Override / Change the constructor signature in SA


    Hi guys,

    how can I modify the constructor of a class in SA?

    If I add a class to a model the constructor is parameterless and I cannot change it. If I add a custom class and add a constructor field, I get an exception. I have resorted to overriding the class (Generate Override) and modified the constuctor here.

    That is what my class looks like:

    Code:
    Ext.define('MyApp.model.Client', {
        extend: 'Ext.data.Model',
        alias: 'model.client',
    
        requires: [
            'MyApp.model.override.Client',
            'Ext.data.Field'
        ],
    
        fields: [
            {
                name: 'firstName'
            },
            {
                name: 'lastName'
            }
        ],
    
        constructor: function() {
            var me = this;
            me.processClient(me);
            me.callParent(arguments);
        },
    
        processClient: function(config) {
            console.log('client created');
        }
    });
    And my overridden class is defined as:
    Code:
    Ext.define('MyApp.model.override.Client', {
        override: 'MyApp.model.Client',
        constructor: function(args) {
            Ext.apply(this, args);
        }
    });
    So this allows me to do:
    Code:
    var myClient = Ext.create('MyApp.model.Client', {
        lastName: 'Mayer',
        firstName:  'Ute'
    });
    Is that the way to go?

    Thanks
    Last edited by aconran; 31 Mar 2014 at 6:46 AM. Reason: add code tags

  2. #2
    Sencha Premium Member
    Join Date
    May 2010
    Location
    Guatemala, Central America
    Posts
    1,261
    Answers
    8
    Vote Rating
    79
    ssamayoa is a jewel in the rough ssamayoa is a jewel in the rough ssamayoa is a jewel in the rough ssamayoa is a jewel in the rough

      0  

    Default


    I not quite sure why you are doing that.

    Even if you don't define any formal parameters in the declaration of a function you can always get the arguments of the function. Example:

    Code:
    function x() {
    alert(arguments[0]);
    }
    
    x(0, 1, 2);
    This will show "0".

    Is the reason that you are creating the override is because you don't see the "config" parameter in the constructor and you want to do something special at construction time?

    If so, no need to create the override class, just be sure to include " this.callParent(arguments)" in your constructor.

    Regards.
    UI: Sencha Architect 3.x / ExtJS 4 & 5
    Server side: JEE / EJB 3.x / CDI / JPA 2.x/ JAX-RS / JasperReports
    Application Server: Glassfish / WildFly
    Databases: Oracle / DB2 / MySQL / Firebird

    If you like my answer please vote!

  3. #3
    Sencha User
    Join Date
    Mar 2014
    Posts
    12
    Answers
    1
    Vote Rating
    0
    FSolutionsDev is on a distinguished road

      0  

    Default Override Constructor

    Override Constructor


    Hi ssamayoa,

    this is just a sort of "learning extjs" sample using Sencha Architect. The idea is to have freedom to initialize a class with the instantiation of the class. So how could I go about instantiating a class with some parameters?. Such as:
    Code:
    MySuperClass = Ext,create('Ext.SuperClass", {
       myConfigObject);
    }
    If I don't use the override. The only way I can see is do something like
    Code:
    MySuperClass = Ext,create('Ext.SuperClass")  //constructor is parameterless
    MySuperClass.ConfigObject = myConfigObject
    This is really Sencha Architect related, where the constructor code section is locked (as far as I can see), except when you override the class (which is kind of nonsense)

    Perhaps the idea is to use the Process function where I have access to the constructor parameters and intialize my values there?

    I might oversee something trivial here, as I am just begining to lear Extjs and SA.

    Thanks
    Last edited by aconran; 31 Mar 2014 at 6:47 AM. Reason: code tags

Thread Participants: 1