I've created an AS class to use as a data model, shown here:
package
{
import mx.controls.Alert;
import mx.rpc.events.FaultEvent;
import mx.rpc.events.ResultEvent;
import mx.rpc.http.HTTPService;
public class Model
{
private var xmlService:HTTPService;
private var _xml:XML;
private var xmlChanged:Boolean = false;
public function Model()
{
}
public function loadXML(url:String):void
{
xmlService = new HTTPService();
if (!url)
xmlService.url = "DATAPOINTS.xml";
else
xmlService.url = url;
xmlService.resultFormat = "e4x";
xmlService.addEventListener(ResultEvent.RESULT, setXML);
xmlService.addEventListener(FaultEvent.FAULT, faultXML);
xmlService.send();
}
private function setXML(event:ResultEvent):void
{
xmlChanged = true;
this._xml = event.result as XML;
}
private function faultXML(event:FaultEvent):void
{
Alert.show("RAF data could not be loaded.");
}
public function get xml():XML
{
return _xml;
}
}
}
And in my main application, I'm initiating the app and calling the loadXML function to get the XML:
<fx:Script>
<![CDATA[
import mx.containers.Form;
import mx.containers.FormItem;
import mx.containers.VBox;
import mx.controls.Alert;
import mx.controls.Button;
import mx.controls.Label;
import mx.controls.Text;
import mx.controls.TextInput;
import spark.components.NavigatorContent;
private function init():void
{
var model:Model = new Model();
model.loadXML(null);
//the following line executes before model.loadXML has finished...
var xml:XML = model.xml;
}
]]>
</fx:Script>
The trouble I'm having is that the getter function is running before loadXML has finished, so the XML varible in my main app comes up undefined in stack traces. Specifically, the loadXML function called ResultEvent.RESULT, then jumping to setXML, etc...the code in the main app continues to execute while loadXML waits for a result, so the getter in the main app (var xml:XML = model.xml;) executes before the variable has been defined by setXML.
How do I put a condition in here somewhere that tells the getter to wait until the loadXML() function has finished before running?