views:

23

answers:

1

Hi guys. I want to dispatch a custom event when the two files are downloaded or uploaded successfully. I use

fileRef.addEventListener(Event.COMPLETE, completeHandler);
fileBigRef.addEventListener(Event.COMPLETE, completeHandler);

to listen the complete event with only 1 handler.

function completeHandler(event:Event):void{
    var e:Event=new Event("addInfoDone");
    dispatchEvent(e);
    fileBigRef.removeEventListener(Event.COMPLETE,completeHandler);
    fileRef.removeEventListener(Event.COMPLETE,completeHandler);
}

I want the event to be dispatched only once when both of the fileRef and fileBigRef are complete transfered. Any ideas?? My brain is fry now and can't think of anything..... Thanks for the help.

+1  A: 

For a quick and dirty solution, how about defining a counter that increments each time completeHandler is fired. Once the counter reaches the number of files you're expecting (sounds like you're expecting 2), you fire your addInfoDone event:

var fileRefCounter:Number = 0;

function completeHandler(event:Event):void{
    fileRefCounter++;
    if(fileRefCounter == 2)
    {
        // both files have downloaded; fire your custom event, or whatever
    }
}
echo
Haha..Nice. Maybe it's a little bit dirty, but it gets the job done..:D Thanks..
Jerry
I was wondering if there are other solution like using event property or something....
Jerry
If you're working with the URLLoader object, you can inspect the `target` property to figure out which of your objects fired the event. Have a look at http://livedocs.adobe.com/flex/3/html/help.html?content=17_Networking_and_communications_3.html
echo
cool..thanks again
Jerry