xmlapache-flexflashactionscript-3e4x

Load xml file in flex before application start/initialises


I've got a configuration xml file that I need to parse for values before a flex application laods.

I've created a static class that allows for the values in the xml config file to be retrieved.

I'm initialising this class when the application first loads but as the xml file is loaded with a Loader class that loads a synchronously the class is being asked for values before the xml file is actually loaded - so it is throwing a error.

Is there a way to load this xml file synchronously or can anyone suggest a work around to this? We cannot embed the file as a class variable as we need to be able to change the values remotely.


Solution

  • You'll want to override the set initialized function.

       <?xml version=”1.0″ encoding=”utf-8″?>
        <mx:Application
            xmlns:mx=”http://www.adobe.com/2006/mxml”
            preinitialize=”preInitHandler(event)”>
    
            <mx:Script>
                <![CDATA[
    
                    private function preInitHandler (event : Event) : void
                    {
                       //load the xml, add the xmlCompleteHandler as a listener
                    }
    
                    private function xmlCompleteHandler (event : Event) : void
                    {
                        //handle the xml
                        super.initialized = true;
                    }
    
                    override public function set initialized (value : Boolean) :
                        void
                    {
                        // don't do anything, so we wait until the xml loads
                    }
    
                ]]>
            </mx:Script>
    
        </mx:Application>