PDA

View Full Version : HOWTO: Pass extra parameters to a callback eg a msgbox



bitdifferent
12 Jun 2007, 7:02 AM
If you are specifying a callback handler and you want it to receive an extra parameter or two, you need to use 'createDelegate'. you can think of it as being like a 'modified copy' of a function which gets scope and extra params from the delegate definition.

For example: imagine you want to get info from the user via two consecutive messageboxes. You ask question 1, and then question 2, and then you do something based on answer 1 and answer 2.

(Obviously there are other, more compact/cleaner ways to show this, I'm just being verbose):



Ext.MessageBox.prompt('Q1', "Question 1", Answer1Submitted);

function Answer1Submitted(btn, text)
{
if (btn=='ok')
{
Ext.MessageBox.prompt('Q2','Question 2', Answer2Submitted.createDelegate(this, {Answer1: text}, true);
}
}

function Answer2Submitted(btn,text,extra)
{
if (btn=='ok')
{
var Answer2=text;
var Answer1=extra.Answer1;
}
}



I hope this helps someone! Docs for createDelegate are here:
http://www.extjs.com/deploy/ext/docs/output/Function.html#createDelegate

And this is a useful thread on this topic:
http://extjs.com/forum/archive/index.php/t-2135.html

Matt S.

mxracer
28 Jun 2007, 5:30 PM
Thank you. This helped me out.

Wolfgang
29 Jun 2007, 10:35 AM
Hello bitdifferent,

i believe that createdelegate and friends are one of the most valuable and most unrecognized functions in the ext library. The problem is to get used with them and to understand what kind of problems they can solve (which requires still a good understanding in programming javascript).
So thank you for the example, it will certainly help people and give them a starting point.

Regards

Wolfgang

jack.slocum
30 Jun 2007, 12:23 AM
Another unknown gem is if you pass a number as the 3rd argument, that becomes the insert position. e.g. if you pass 0, it will be the first argument.

manugoel2003
30 Jun 2007, 3:35 AM
wow Jack... didn't knew it.... could be very helpful at times.... u mean instead of passing true just pass a number in the third argument, right?