How to get associated URLRequest from Event.COMPLETE fired by URLLoader
- by matt lohkamp
So let's say we want to load some XML -
var xmlURL:String = 'content.xml';
var xmlURLRequest:URLRequest = new URLRequest(xmlURL);
var xmlURLLoader:URLLoader = new URLLoader(xmlURLRequest);
xmlURLLoader.addEventListener(Event.COMPLETE, function(e:Event):void{
trace('loaded',xmlURL);
trace(XML(e.target.data));
});
If we need to know the source URL for that particular XML doc, we've got that variable to tell us, right? Now let's imagine that the xmlURL variable isn't around to help us - maybe we want to load 3 XML docs, named in sequence, and we want to use throwaway variables inside of a for-loop:
for(var i:uint = 3; i > 0; i--){
var xmlURLLoader:URLLoader = new URLLoader(new URLRequest('content'+i+'.xml'));
xmlURLLoader.addEventListener(Event.COMPLETE, function(e:Event):void{
trace(e.target.src); // I wish this worked...
trace(XML(e.target.data));
});
}
Suddenly it's not so easy, right?
I hate that you can't just say e.target.src or whatever - is there a good way to associate URLLoaders with the URL they loaded data from? Am I missing something? It feels unintuitive to me.