8 Replies Latest reply on Oct 13, 2009 2:49 PM by msakrejda

    Wait for event handler to finish

    Guddie

      What I would like to do is have my Flex application read the contents of an XML file, and use the contents to set some variables in the application, before any of the components actually load up. So it will be like a config file, where constants can be specified that the application uses. But the application must read the XML file and initialize the constants before loading. The preinitialize event is where I put the code to load the XML, and in the result handler I am setting the variables from the XML contents. But the application sometimes loads faster than the result handler, but the result handler needs to finish first. I'm using an HTTPService component.

       

      Any ideas or help would be greatly appreciated.

        • 1. Re: Wait for event handler to finish
          Gregory Lafrance Level 6

          Set a global Boolean variable to false and bind the components "visible" property to the variable. Then after the data is loaded, set the variable to true and your components will appear. If you don't even want the UI to layout considering the components, set their includeInLayout property to the global variable as well.

           

          If this post answers your question or helps, please mark it as such.

          • 2. Re: Wait for event handler to finish
            Guddie Level 1

            Thanks for the response. To clarify, a component's visibility is not the issue. The issue is the loading of the entire application itself (again, not visibility). I need some way to suspend the loading of the rest of the application until the result handler function finishes executing, or some workaround to accomplish this.

             

            Again, any help is greatly appreciated.

            • 3. Re: Wait for event handler to finish
              WordRad

              Don't know if this helps you or not (or even if the following is considered kosher):

               

              <?xml version="1.0" encoding="UTF-8"?>
              <mx:Application
                xmlns:mx="http://www.adobe.com/2006/mxml
              ...

                initialize="{InitWR()}"
              >

               

              ...

               

                  private function InitWR():void {           
                       
                    fnCFG = this.parameters.cfg;
                   
                  ...    

                   
                    var request:URLRequest = new URLRequest(fnCFG);
                    var loader:URLLoader = new URLLoader();
                    loader.addEventListener("ioError", InitWR1);
                    loader.addEventListener("securityError", InitWR1);
                    loader.addEventListener(Event.COMPLETE, InitWR1);
                    loader.load(request);

                  ---


                  }

               

              ---

                  private function InitWR1(event:Event):void {


                    if (!URLLoader(event.target).bytesLoaded) {
                      err_NF.push(fnCFG); 
                      return;
                    }

                  

                  cfg = new XML(URLLoader(event.target).data);

                    img_bg = new SWFLoader();
                    img_bg.visible = false;
                    img_bg.addEventListener("ioError", InitWR2);
                    img_bg.addEventListener("securityError", InitWR2);
                    img_bg.addEventListener(Event.COMPLETE, InitWR2);
                    img_bg.load(rel_path+cfg.textr_dir.@path + cfg.textr_bg.@source);


                  }

               

                ---

               

                  private function InitWR2(event:Event):void {

                    var fn:String = rel_path+cfg.textr_dir.@path + cfg.textr_bg.@source;
                    if (isNaN(img_bg.bytesLoaded)) err_NF.push(fn);

                    if (!isNaN(img_bg.bytesLoaded)) {
                      CanvasSB.graphics.clear(); 
                      CanvasSB.graphics.beginBitmapFill(Bitmap(img_bg.content).bitmapData); 
                      CanvasSB.graphics.drawRect(0,0,CanvasSB.width,CanvasSB.height);   
                      CanvasSB.graphics.endFill();   
                    }

                    CanvasSB.visible = true;


                    img_fg = new SWFLoader();
                    img_fg.visible = false;
                    img_fg.addEventListener("ioError", InitWR3);
                    img_fg.addEventListener("securityError", InitWR3);
                    img_fg.addEventListener(Event.COMPLETE, InitWR3);
                    img_fg.load(rel_path+cfg.textr_dir.@path + cfg.textr_fg.@source);

               

                  }

               

              (And then there's an InitWR3 and InitWR4 as well)

              • 4. Re: Wait for event handler to finish
                Guddie Level 1

                Thanks for the response. Unfortunately I don't think that this will solve my problem.

                • 5. Re: Wait for event handler to finish
                  WordRad Level 1

                  Out of curiousity, what did I miss regarding the nature of your problem.

                  • 6. Re: Wait for event handler to finish
                    Guddie Level 1

                    It's basically what I'm doing now... just adding event listeners and handling them. I need some way to actually suspend loading the rest of my application while I wait for one of the event handlers to complete it's function, or something that does the same thing.

                    • 7. Re: Wait for event handler to finish
                      WordRad Level 1

                      Well , my code is loading attributes of the application incrementally - reading something out of the XML file, then going and loading something, waiting for it to load, then reading the next thing out of the xml file, then loading something in response, waiting for it to load etc.

                      • 8. Re: Wait for event handler to finish
                        msakrejda Level 4

                        Put the bulk of your application in a separate component, and only instantiate that and add it as a child of the application (either programmatically or through a state transition) once your handler returns.