PDA

View Full Version : Call function of containing object



mheinrich
5 Jan 2011, 11:00 AM
Hi there,

is there any possibility to reach a function from a button handler which is defined in a class that contains the button as an item?

This code may illustrate what I mean:




...
items: [
{
xtype: 'button',
handler: function(btn, e){ /* I want to reach dummy from here */ }
}
],

dummy: function(){ /* useful code in here*/}
....
Please note that this is supposed to be a class definition. No instance, so I can't just use an id or something.

Thanks in advance,

Markus

skirtle
5 Jan 2011, 3:10 PM
Without more context it's a little hard to say. Something like this is normal:


MyClass = Ext.extend(..., {
initComponent: function() {
this.items = [
{
xtype: 'button',
handler: function(btn, e) {/* call to this.dummy(); */},
scope: this
}
];

MyClass.superclass.initComponent.call(this);
},

dummy: function() {...}
});It's also generally considered good practice to put handlers on the prototype to avoid creating a new closure for each instance:


MyClass = Ext.extend(..., {
initComponent: function() {
this.items = [
{
xtype: 'button',
handler: this.onButtonClickHandler,
scope: this
}
];

MyClass.superclass.initComponent.call(this);
},

onButtonClickHandler: function(btn, e) {/* call to this.dummy(); */},

dummy: function() {...}
});

mheinrich
6 Jan 2011, 2:22 AM
Thank you. Your example is exactly what I need.

Markus