Reputation: 387
I'm executing the following code
var myXMLURL:URLRequest = new URLRequest("config.xml");
myLoader = new URLLoader(myXMLURL); // implicitly calls the load method here
myLoader.addEventListener(Event.COMPLETE, xmlLoaded);
myLoader.addEventListener(IOErrorEvent.IO_ERROR, errorHandler);
The URLloader executes as soon as you create it with a URLRequest.
My question is I'm adding an eventlistener after that statement, it currently catches the Event.Complete event, will this continue to work in the future? or should the eventListeners be added before the load is called?
Upvotes: 0
Views: 240
Reputation: 8159
If you are worried about this, don't load in the constructor. The URLRequest
in the constructor is optional.
So do this:
var myXMLURL:URLRequest = new URLRequest("config.xml");
myLoader = new URLLoader();
myLoader.addEventListener(Event.COMPLETE, xmlLoaded);
myLoader.addEventListener(IOErrorEvent.IO_ERROR, errorHandler);
myLoader.load( myXMLURL );
This is the way I handle all URLLoader
s. I like being in complete control of my code, so not setting the URLRequest in the constructor gives me the freedom to call the load()
when I so choose, as well as giving me the option to add event listeners before the load begins. The fact the URLLoader allows for auto loading in the contructor has always baffled me, to be honest. It goes completely against how Adobe handles constructors throughout the SDK.
Upvotes: 1