I am attempting to build a generic XMLLoader class with a static LOAD
method, with the intent to use it as follows...
private var _data:XML = XMLLoader.LOAD("path/to/xml_file.xml");
Then I could use it in any client and go-to-town with e4x.
The general problem I am having is with the URLLoader's COMPLETE
event, which necessarily calls a different function to set the XML data.
This is preventing me from being able to return the XML from the LOAD
method, since the data is being set outside that function. Obviously I need to wait for the COMPLETE
event in order to make sure the data is available.
I though, perhaps, I could create and return a _waitForData
function, which recursively calls itself until the _data is set, then returns the data. But it seems redundant (since Event.COMPLETE
is doing that anyway), and the way I tried it generates a stack overflow error.
Heres what I tried:
public class XMLLoader {
private static var _url:String = "";
private static var _data:XML = null;
public static function LOAD(url:String):XML {
_url = url;
var _xmlLoader:URLLoader = new URLLoader();
_xmlLoader.addEventListener(Event.COMPLETE, _setData);
_xmlLoader.load(new URLRequest(_url));
return _waitForData();
}
static function _setData(e:Event):void {
XML.ignoreWhitespace = true;
_data = new XML(e.target.data);
}
static function _waitForData():XML {
if( _data == null ) {
_waitForData();
}
return _data;
}
public function LoadXML() {}
}
I pretty new to the concept of recursive functions, so I have a feeling I implemented my _waitForData function incorrectly (if its even a good idea at all).
I would appreciate any pointers on using static methods in this way, and whether what I am trying to do is possible...seems like a good idea I think.