views:

52

answers:

1

Below is my class, which simply reads an xml file and provides the contents in e4x format. Unfortunately, after the constructors executes and sets the xmlProperties property with the expected values, it some how becomes null. Anyone know what I'm doing wrong?

    public class WebService
    {
    private var _propertiesReader:HTTPService;
    private var _xmlProperties:XML;

    public function WebService()
    {
        _propertiesReader = new HTTPService();
        _propertiesReader.url = "../resources/properties.xml";
        _propertiesReader.resultFormat = "e4x";
        _propertiesReader.contentType = "application/xml";
        _propertiesReader.addEventListener(ResultEvent.RESULT, function(event:ResultEvent):void
        {
            _xmlProperties = XML(event.result);
        });
        _propertiesReader.addEventListener(FaultEvent.FAULT, function(event:FaultEvent):void 
        {
            Alert.show("Unable to load properties content: " + event.fault.message + "\nPlease try again later.", "Properties File Load Error");    
        });
        _propertiesReader.send();
    }

    public function get xmlProperties():XML
    {
        return _xmlProperties;
    }
    }
+1  A: 

_xmlProperties is being set by a File Load call (via a callback event). It is not being set directly in the constructor.

Are you sure you are waiting for the call to finish and the callback event to fire before you check the value of _xmlProperty?

Justin Niessner
Is there a way to synchronize the callback? Currently, I instantiate the class and then immediately attempt to retrieve the _xmlProperty.
tommac
@tommac: You should rather listen to the event and only use it after the event has fired. This is how event-based programming works.
Matti Virkkunen
@Matti: There in lies the trouble; if I were to listen for the event to fire I'd be coupling this class to whatever class/script I called it from. My goal is to encapsulate reading from the properties file.
tommac
@tommac: Exposing an event doesn't strongly couple anything together because you can listen for the event from anywhere you want.
Matti Virkkunen