Search code examples
inheritanceextjsextjs7

ExtJS inheritance issue with multiple instances of an extending class


I have multiple forms that all start with the same, "universal" fields but then have different fields depending on the form. As such, I have a parent form that all the child forms are extended from. The parent form adds the universal fields so I don't have to re-declare those fields in the child forms.

The issue that I'm having is when I have multiple instances of the same child form, those univeral fields are re-added for each instance. Might sound a little confusing but I think the following fiddle should make it pretty clear: https://fiddle.sencha.com/#fiddle/36lu&view/editor. Obviously I'm doing this incorrectly so just trying to figure out what I'm doing wrong.

Code for the parent/child classes:

Ext.define('TestParentForm', {
    extend: 'Ext.form.Panel',
    xtype: 'testparentform',

    initComponent: function(){
        var me = this;

        if(!me.items){
            me.items = [];
        }

        Ext.Array.insert(me.items, 0, [
            {
                xtype: 'textfield',
                fieldLabel: 'Universal Parent Field'
            }
        ]);

        me.callParent(arguments);
    }
});


Ext.define('TestChildForm', {
    extend: 'TestParentForm',
    xtype: 'testchildform',
    items: [
        {
            xtype: 'textfield',
            fieldLabel: 'Child-specific Field'
        }
    ]
});

Solution

  • I fixed this by defining items in the child class using the initComponent function. See fiddle here: https://fiddle.sencha.com/#fiddle/36nn&view/editor

    Ext.define('TestParentForm', {
        extend: 'Ext.form.Panel',
        xtype: 'testparentform',
    
        initComponent: function(){
            var me = this;
    
            if(!me.items){
                me.items = [];
            }
    
            Ext.Array.insert(me.items, 0, [
                {
                    xtype: 'textfield',
                    fieldLabel: 'Universal Parent Field'
                }
            ]);
    
            me.callParent(arguments);
        }
    });
    
    
    Ext.define('TestChildForm', {
        extend: 'TestParentForm',
        xtype: 'testchildform',
    
        initComponent: function(){
            var me = this;
    
            me.items = [{
                xtype: 'textfield',
                fieldLabel: 'Child-specific Field'
            }];
    
            me.callParent(arguments);
        }
    });