Fábio Antunes
Fábio Antunes

Reputation: 17164

HTTPService event listener so it triggers something when ".send" method gets data from XML

Good day everybody.

I've doing a bit of "training" at working with Flex and Remote Data from XML files.

This is my HTTPService

<mx:HTTPService id="loginData" url="com-handler/basic.xml" showBusyCursor="true">
</mx:HTTPService>

I have a button and when its clicked its call a function, that calls loginData.send and does a little IF condition, that IF condition relies on the data returned by loginData.

The condition doesn't work because its called right next to the loginData.send, and .send method still didn't returned the values from the XML file. But if you click it a second time a second after the first click the IF condition works.

So to deal with i wanted to do a eventListener so that when loginData.send returned the data from the XML it fires up the IF condition. But i don't know how to do it.

Can you help me?

Upvotes: 1

Views: 1259

Answers (1)

Joel Hooks
Joel Hooks

Reputation: 6565

The send method of HTTPService returns an AsyncToken, to which you can add a Responder, as well as arbitrary data. So in the click handler of your Button:

var responder:IResponder = new Responder(myResultFunction, myFaultFunction)
var token:AsyncToken = myService.send();
token.addResponder(responder);

Additionally, the MXML can declare a result handler:

<mx:HTTPService id="myService" result="myResultHandler(event)"/>

One interesting aspect of AsyncToken is that it is a dynamic object, meaning you ca apply arbitrary properties to it:

var responder:IResponder = new Responder(myResultFunction, myFaultFunction)
var token:AsyncToken = myService.send();
token.addResponder(responder);
token.myArbitraryProperty = "Whatever";
token.anotherProperty = someObject;

Now, in the myResultFunction you can access event.token.myArbitraryProperty for use in conditionals or whatever you may need.

Upvotes: 3

Related Questions