A little (!) bit of background before I can get to the question :
I have an accordion control loaded with an array of grids, each of which is lazy loaded with arrays of things. I'm using an auto-generated web service proxy to retrieve these lists. I'd like for the user to be able to change the selected child in the accordion without having to wait for the web service to respond. I was originally using the same proxy instance for all requests and keeping track of the requests in the order they were made, but the problem with this is that shorter arrays return more quickly from the server, so the order the requests were made in becomes irrelevant.
I couldn't find an obvious way to determine the original request when handling a proxy result event so what I ended up with is a function which handles the change event on the accordion, instantiates a new webservice proxy, shoves that into a hashtable with the index of the selected child and then adds a closure as an event handler. i.e. something a bit like this :
private proxyTable:Object = new Object();
private function PopulateThingGrid(index:Number):void
{
var grid:ThingGrid = myAccordion.getChildAt(index) as ThingGrid;
grid.things = ArrayCollection(proxyTable[index].getThings_lastResult);
}
private function SendThingRequest(index:int):void
{
var grid:ThingGrid= myAccordion.getChildAt(index) as ThingGrid;
if (grid.things.length == 0)
{
if (proxyTable[index] == null)
{
proxyTable[index] = new MyWebServiceProxy();
}
var proxy:MyWebServiceProxy= proxyTable[index];
proxy.addgetThingsEventListener(function ():void { PopulateThingGrid(index); });
var list:ThingList = thingLists.getItemAt(index) as ThingList;
proxy.getThings("thinglist", list.ListID);
}
}
private function myAccordion_Change(event:IndexChangedEvent):void
{
SendThingRequest(event.newIndex);
}
(I've tried to anonymise this a bit, so I may have missed something, but hopefully you get the idea)
So, to the question(s) : is there an easier way to match up proxy results with the original requests that I'm just missing?
If not, is what I've done reasonable? I'm a little concerned about the number of proxy instances that I could end up generating and then disposing of them correctly (when that becomes necessary) - are there any pitfalls I might not be aware of?
Update : I think the problem may arise because the generated proxy code subclasses the ResultEvents from flash.events.Event, rather than mx.rpc.events.ResultEvent. I'm not entirely sure why it does this - the only way to access the AsyncToken is when it's initially returned by the method call.