My question is very simple: In flex3, is there a way to load an xml file synchronously?
I know how to load asynchronously, using a load event. This may be useful, or may not. I just want to read the file, parse it, do what I have to do with it, and continue executing code.
I have a component that uses an xml file to store some configuration parameters. I need to read the file when the object is initialized. However, with the event model, I can't control when the file is loaded, so I must write code to "wait" for the code to load. This is just ridiculous, or is it me? I want code like this:
var foo:Foo = new Foo(); //This constructor should read the xml and initialize the object.
foo.doSomething(); //When I call this method the xml must be already handled.
I can handle the xml file on the event, and it works fine, but the event fires after the doSomething method.
I hope I have explained myself. I think this should be really easy, but it's driving me crazy. I don't want to write code to wait for the event unless it's really necessary. I feel all this should be just one line of code!
It's not possible to load synchronously, flash is built for the web and you can never be sure how long a call takes. Air is different because that loads from the filesystem, and there are nowhere near the same amounts of delay there.
The cleanest solution would be to listen for the load to complete inside Foo and calling doSomething() from inside, this way your "outer" class won't need to bother at all.
If you do absolutely need to call foo.doSomething() from the outside, you can use the event system. Let your Foo class dispatch an event when it is done loading:
dispatchEvent(new Event(Event.COMPLETE, true));
To catch that you will need to listen for it like so:
foo.addEventListener(Event.COMPLETE, handleFooComplete);
And you event handler function should look like this:
private function handleFooComplete(e:Event):void{
foo.doSomething();
}
However you choose to do it, you will need to listen for Event.COMPLETE on the loader. There's no getting around that.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With