Flex 4: Getter getting before setter sets
- by Steve
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);
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. How do I put a condition in here somewhere that tells the getter to wait until the loadXML() function has finished before running?