PDA

View Full Version : extending of parent class doesn't work



kondorek
15 Jul 2014, 2:43 AM
Hi, could someone of you please take a look at this both peaces of code, and tell me why my childClass doesn't work. It doesn't show my another items (someAnotherItem,someAnotherItem2) , instead there are always called parent items (someItem,someItem2), when i try to use myChildClass.
I can see console.log('yuhu')


parent
Ext4.define('MyParentClass', {
extend : 'Ext4.Panel',
alias : 'widget.myParentClass',

/**
* Initialize configuration.
*/
initComponent : function() {
Ext4.apply(this, {
layout: 'border',
items: [{
xtype: 'someItem',
region: 'north',
layout: 'fit',
collapsible: true,
split: true
},{
xtype: 'someItem2',
layout: 'fit',
region: 'center'
}]
});

this.callParent(arguments);
this.on('added',this.initListeners, this);
},
/**
* initialize events
*/
initListeners: function(component, container){
/*some events*/
}

});

and my child class

Ext4.define('MyChildClass', {
extend : 'MyParentClass',
alias : 'widget.myChildClass',

/**
* Initialize configuration.
*/
initComponent : function() {
Ext4.apply(this, {
items: [{
xtype: 'someAnotherItem',
title: this.i18n.title,
collapsible: false
},{
xtype: 'someAnotherItem2'
}]
});
console.log('yuhu');
this.callParent(arguments);
}

});

Thanks for your help
regards
Kondorek

skirtle
15 Jul 2014, 3:44 AM
The apply method just copies properties from one object to another. Your child class sets items on this and then calls callParent, which will call the parent class's initComponent. That will change the value of items on this.

You could use applyIf instead, or add a template method that returns the items config rather than setting it directly in both classes.

kondorek
15 Jul 2014, 6:09 AM
thank you @skirtle applyIf seams doesn't work properly in my case.
I try to use some template but i'm affraid i've to use the worst method and create exactly copy of my parent class. It's not pretty but it 100% works :)

kondorek
15 Jul 2014, 6:12 AM
thanks skirtle, but applyIf seams not to work :)
i try to use templates but in worst case I'll be forced to repeat the code:((

skirtle
15 Jul 2014, 10:07 AM
I've explained the template method approach here:

http://skirtlesden.com/articles/config-objects-on-the-prototype

kondorek
15 Jul 2014, 10:50 PM
that looks great, thanks skirtle