View Full Version : HTTPService and closure
jhapak
05-18-2007, 10:42 PM
I am trying to implement closure example I found on a website somewhere but my alert is not working. That means call.handler is not executing properly. Here is the code:
public function getSomething(url:String):Boolean
{
var httpService:HTTPService = new HTTPService();
var call:Object;
httpService.url = url;
httpService.send();
call = httpService.send();
call.handler = function (event:Event):void
{
Alert.show("I am here");
}
return true;
}
Can somebody tell me where is the error? I am new to actionscript and flex.
dr_zeus
05-18-2007, 11:45 PM
You haven't told the HTTPService to use your "handler" function to actually handle the event it sends. Also, you're calling httpService.send() twice. Here's something that should work better.
public function getSomething(url:String):void
{
var httpService:HTTPService = new HTTPService();
httpService.url = url;
//you need to tell the service who's listening
httpService.addEventListener(ResultEvent.RESULT, resultHandler);
httpService.send();
}
private function resultHandler(event:ResultEvent):void
{
var httpService:HTTPService = event.target as HTTPService;
//don't forget to stop listening. we don't want memory leaks!
httpService.removeEventListener(ResultEvent.RESULT , resultHandler);
Alert.show("I am here");
}
jhapak
05-19-2007, 09:34 AM
Thanks for the reply and the tip for the memory leak. I didn't know that at all.
The other thing why I am trying to use closures is because I want to return the values that I am getting from http send. This is means I have an actionscript class that is being called by mxml. And on button click, it should return me some value.
Regards
jhapak
05-19-2007, 09:52 AM
Ok I found that link again:)
http://kuwamoto.org/2006/05/16/dealing-with-asynchronous-events-part-2/
Please refer to this link for closures. Do you have any idea now why my code is not working?
Thanks in advance.
dr_zeus
05-21-2007, 06:50 PM
That article was written before the official release of Flex 2. It's possible that the API changed between the time it was written and the actual release. I know that HTTPServices return something called an AsyncToken, and you cann to add a responder (that implements the IResponder interface).
|
vBulletin® v3.8.4, Copyright ©2000-2009, Jelsoft Enterprises Ltd.