views:

1306

answers:

6

Hi,

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.

Any ideas?

Regards

Jon.

A: 

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.

bug-a-lot
+7  A: 

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>
quoo
Thank you very much for this worked like a charm.
Jon
A: 
A: 

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>
Jami
A: 

Hi Jon,

I'm struggling with the same problem for a while now. I tried using the creationPolicy attribute to suspend the creation of components, but ended up with a stuck preloader. I've also tried to override set initialized function, but still got some race conditions with other objects using the configuration.

Did you find a good solution for your problem?

Regards, Shahar

Shahar
A: 

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

Mark Murphy