PDA

View Full Version : what is "this" in toolbar button click event



Bogdan0x400
21 Feb 2011, 12:46 AM
I have an editorgrid and a toolbar with a button. What gets passed as this parameter when the user clicks the toolbar button? I get DispHTMLWindow2 as this parameter, but I can't figure out for what Ext component it stands.

evant
21 Feb 2011, 12:48 AM
http://dev.sencha.com/deploy/dev/docs/?class=Ext.Button&member=scope



The scope (this reference) in which the handler and toggleHandler is executed. Defaults to this Button.

Bogdan0x400
21 Feb 2011, 1:17 AM
So if I have code like:


var grid = new Ext.grid.EditorgridPanel({
tbar: [
{
text: 'New',
handler: function () {
alert("toolbar button clicked");
},
scope: this
}
]
});

then in the handler function I'll get a grid reference in this, right?

Condor
21 Feb 2011, 4:54 AM
No. 'this' will be the value of 'this' at the time you created the grid (it's evaluated before the grid is created!).

If you want a grid reference, then I would recommend:

handler: function(btn) {
var grid = btn.findParentByType('grid');
...
}

Bogdan0x400
21 Feb 2011, 6:18 AM
Thanks for the answers, Condor and Evant!