Why not just use a single HTTPService instance? The send() method returns
an "AsyncToken", a dynamic object to which you can add any property and
value you want. I set a "called" to a string that describes the source of
the call. Sometimes I also include a "nextaction" property if I need more
control.
Then in the single result handler, I read the called value, and using a
switch statement, process it as needed. If call and result sequence is
important, I invoke the next call in the case clause for the previous call.
If the calls do not need to be in sequence, just complete, track the calls
in an array or hashtable(Object). Have the send() for each call put the
callid in the hashtable. Have the result handler remove the called, and
check to see if any remain. If not, proceed with your function call.
Better have the fault handler decrement the hashtable/array as well.
This makes for easy debugging and maintenance of data service calls, since
all come through the same function. Here are some sample code snippets.
Sample code using HTTPService, e4x, handler function to populate a DataGrid.
Also shows usage of AsyncToken.
The DataGrid tag:
<mx:DataGrid id="dg" dataProvider="{_xlcMyListData}" .../>
The HTTPService tag:
<mx:HTTPService id="service" resultFormat="e4x" result="onResult(event)"
fault="..../>
Script block declaration:
import mx.rpc.Events.ResultEvent;
import mx.rpc.AsyncToken;
[Bindable]private var _xlcMyListData:XMLListCollection;
Invoke send:
var oRequest:Object = new Object();
oRequest.Arg1 = "value1";
var callToken:AsyncToken = service.send(oRequest);
callToken.callId = "myQuery1";
Result Handler function:
private function onResult(oEvent:ResultEvent):void {
var xmlResult:XML = XML(event.result); //converts result
Object to XML. can also use "as" operator
var xlMyListData:XMLList = xmlResult.myListData; //depends on xml
format, is row data
_xlcMyListData = new XMLListCollection(xlMyListData); //wrap the XMLList
in a collection
trace(_xlcMyListData.toXMLString()); //so you can see
exactly how to specify dataField or build labelFunction
var callToken:AsyncToken = oEvent.token;
var sCallId = callToken.callId; //"myQuery1"
switch(sCallId) { //Process the result
conditionally
case "myQuery1":
doQuery2(); //do whatever. this
example calls another data service query
break;
...
}
}//onResult
Tracy Spratt,
Lariat Services, development services available
_____
From: [email protected] [mailto:[email protected]] On
Behalf Of claudiu ursica
Sent: Wednesday, March 25, 2009 2:59 PM
To: [email protected]
Subject: Re: [flexcoders] working with multiple httpservice requests
Make a class with 2 httpservice instances and use a scheduler (have to write
logic for it though). You won't be able to run more than 2 parallel
connections anyway, the browser won't let you... you have to implement
result and fault functions for the calls in there also...
hth,
Claudiu
_____
From: "Ramsey, Robert L" <[email protected]>
To: "[email protected]" <[email protected]>
Sent: Wednesday, March 25, 2009 8:44:26 PM
Subject: [flexcoders] working with multiple httpservice requests
Hi,
I've been trying to find a better way to handle working with multiple
httpservice requests that all need to complete before I run a function.
What I've been doing is something like this:
<mx:HTTPService id="s1" url="somewhere" result="s2.send()"/>
<mx:HTTPService id="s2" url="somewhere" result="s3.send()"/>
<mx:HTTPService id="s3" url="somewhere" result="s4.send()"/>
<mx:HTTPService id="s4" url="somewhere" result="s5.send()"/>
<mx:HTTPService id="s5" url="somewhere" result="somefunction( )"/>
What I'd like to do is have a function I can call as part of the
applications creationComplete to run through all of them.
function initializeApp( ):void
{
s1.send();
s2.send();
s3.send();
s4.send();
s5.send();
somefunction( );
}
I know that somefunction( ) would have to do a valid data check to make sure
that I had result data before manipulating it. Is there a best practices
way of doing this?
Thanks,
Bob