PDA

View Full Version : Custom arguments to an event handler?



pret
23 Nov 2010, 2:04 PM
Hi everyone!

I'm sure more than one guy must have asked about this but I did a search here and on Google and I just found one answer that didn't work, so I'm sorry if this is a dumb question.

I need to handle a click event on a TreePanel:



myTreePanel.on( 'click', myHandler );


And in that handler I need to do an AJAX request to look for an HTML from the url that I have set on the node:



myHandler( node, eventObject )
{
Ext.Ajax.request( {
url: node.attributes.url,
success: function( response, options )
{
// Here I load the HTML on another panel, but I need to receive the node!
}.createDelegate( this )
} );
}


As the comment says, I need to receive the node on the success handler too. I've found that the answer is to use the "createDelegate" method, but I couldn't get it to work. How do I pass the node object from the "click" event handler of the TreePanel, to the "success" event handler of the AJAX request?

And BTW, Can I load the HTML directly on a simple Panel? (not a TabPanel).


Thanks in advance!

plalx
23 Nov 2010, 2:24 PM
Hi,

The node and event object will get passed automatically to your handler function when the event will get fired, so your current handler function is well defined function myHandler (node, e) {}. Then, to access the node and e variables in the success handler, you don't have to pass anything because these will be wrapped in a closure and will be accessible in the function. You should read about javascript closures to understand this concept better, it's a must!

E.g.



function myHandler(node, e) {
Ext.Ajax.request({
...,
success: function(response, options) {
console.log(node);
}
});
}

pret
23 Nov 2010, 2:54 PM
Ooops, I didn't know that. I've read about closures but it seems that detail wasn't there :P I need to read more about them.

Anyway and just in case for another moment I need it: Suppose that I want to pass another argument through the "createDelegate" method. How could I do that?


Thanks a lot for your help!