Load xml file in flex before application start/initialises

余生颓废 提交于 2019-12-22 09:31:19

问题


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.


回答1:


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>



回答2:


How about using states? Default state shows a spinner while the xml loads, and the handler for the complete event of the loading process changes to another state that removes the spinner and adds your main container.

And no, you cannot load a file synchronously in Flex.




回答3:


Nice trick, Quoo, but... One have to handle the case where XML file has been loaded BEFORE frameworks call initialized = true.

Something like this : private var _fileLoaded:Boolean =false;
private var _initialized:Boolean =false;

private function xmlCompleteHandler (event :Event) :void
//handle the xml
_fileLoaded = true;
super.initialized =_fileLoaded && _initialized;
}
override public function set initialized (value : Boolean) :void{
_initialized = value;
super.initialized =_fileLoaded && _initialized;

}




回答4:


I have found that overriding the initialized property does not handle very well when the application is put online.

Instead, you're better off using the property creationPolicy. When set to 'none', this property puts on hold the child creation of the container until the method createComponentsFromDescriptors is called.

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" 
                layout="absolute"
                preinitialize="{loadStuff();}"
                creationPolicy="none">

<mx:Script>
    <![CDATA[

        private function loadStuff():void {
            // Load your stuff here
        }

        private function loadStuffHandler(event:Event):void {
            // This should be called when loading (from loadStuff method) finishes
            createComponentsFromDescriptors();
        }

    ]]>
</mx:Script>

</mx:Application>



回答5:


re: Jami ... createComponentsFromDescriptors(); is now createDeferredContent();



来源:https://stackoverflow.com/questions/1164257/load-xml-file-in-flex-before-application-start-initialises

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!