0

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?

stevemcl
  • 367
  • 1
  • 3
  • 11
  • I'm pretty sure the URLLoader should not load the remote URL until you call the load() method; which you are not doing. Based on code you provided I would not expect either the xmlLoaded or errorHandler to ever get executed. That said, it doesn't matter when you add an event listener to a class instance. If the class dispatches the event the event handler method should get called. – JeffryHouser Apr 19 '13 at 18:28
  • @www.Flextras.com See the [LiveDocs](http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/net/URLLoader.html#URLLoader()). " If specified, the load operation begins immediately (see the load entry for more information)." – Josh Apr 20 '13 at 00:40

1 Answers1

1

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 URLLoaders. 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.

Josh
  • 8,079
  • 3
  • 24
  • 49