Monday, June 8, 2009

CallResponders explained..

Here is some explanation about CallResponders by Tom Lane. I thought this would be useful to many developers who want to understand more about them so that it becomes easy to work with them.

"Let me take a shot at explaining asynchronous calls. Most PHP developers are used to calling functions in one way only: synchronously. In ActionScript a sychronous function call looks like this:

var stuff = getStuff();

Pretty straightforward. It's how you expect functions to work: you call them, and then immediately on line #2 of this example you have a value in the stuff variable to work with.

But when it comes to calling remote functions on services sitting on a server somewhere, Flex calls them asynchronously. This is so that the Flex client can continue working while the request is pending. An aync call looks more like this (at least, the first part):

responder.token = getStuffFromService();

What this means is, "stuff" does not come back right away as the result of the function. Instead, what it returns is a kind of ticket. Sort of like ordering some food from a lunch counter and getting a receipt with a number on it. You don't get your food right away. Instead, you go hang out or do some other stuff while your order is pending, and then your number is called when it's ready. And then you "respond" to your number.

Well, that's what a CallResponder is. When you "place your order", you hook up a CallResponder to it. When your order is ready (data comes back from the service call), the responder kicks into action. It fires a result event (or a fault if something went wrong), and it updates its bindable lastResult property.

So, if you want to work with data from an async call, you can't simply do it on line #2 of the above snippet. You have to wait until the CallResponder for that call gets its result. If you create a databinding to lastResult, that will automatically update when the result is ready. But since you want to run some script on the result, you have to trigger that script from the CallResponder's result event handler.

Hope this helps!

10 comments:

DEVSACHIN said...

Thanks, It really helps to me. I am calling 3 web services one-by-one.Currently i have one web service on result event of another web service.

Regards
Sachin
http://devsachin.wordpress.com/

Samruddhi Parmar said...

Hello,
I have called the lastResult property at the button click event in my application. But the lastResult always gives a null value on first click of button. On clicking the second time i get the values returned from my wsdl. I have been stuck with this since 3 days. Please help.

Thanks in advance,
Samruddhi

DEVSACHIN said...

Hi Samruddhi,
Where are you calling your webservice? In button click or creationComplete of Application?

Thanks
Sachin

udara said...

thanks,this is a very nice explanation of CallResponder.It helps me a lot.Thanx again

Chaitanya said...

nice one..

Nathan Trenholm said...

Thank you soooo much!!! That is a great analogy that has completely cleared up the problem I was having. You have wiped away the fog from my glasses.

I had a problem that seems similar to Sumruddhi's. I was trying to update a label after a click event on a datagrid was triggering my data service and callResponder, but it was always a step behind. After the analogy I realize that the label was getting old info from the callResponder and not waiting for it to get back the information from the database. Now I use the datagrid click event to call the dataservice and the result event from the callresponder to update the label. This now gives me the most current info in the label.

I would not have figured out the problem without the lunch counter analogy. Thanks!!!

Paullo Estevam said...

Buddy, and what´s the difference in CallResponder and the RemoteObject?

RO have event result too.

Sreenivas said...

RemoteObject represents the object on the server side allowing the client to call server methods. CallResponder represents one such call.

tab2600 said...

This is probably a silly question but I am new to actionscript and flex. How do you setup the return result event? Are you modifying the generated code or defining a new event somehow?

tab2600 said...

I answered my own question.

protected function button_clickHandler(event:MouseEvent):void
{
AuthResult.token = login.Auth(usernameTextInput.text,passwordTextInput.text);
AuthResult.addEventListener(ResultEvent.RESULT,result_Handler);
}

protected function result_Handler(event:ResultEvent):void
{
if(AuthResult.lastResult == true)
{
this.lblStatus.text = "Success";
}
else
{
this.lblStatus.text = "Fail.";
}
}