7 Replies Latest reply on Sep 7, 2009 9:55 PM by poojagupta

    Can I add multiple function calls in CreationComplete of Application?

    poojagupta Level 1

      Hi,

       

      I need to call two functions when the application launches. How can i do this?

       

      Thanks in advance.

       

      Pooja

        • 1. Re: Can I add multiple function calls in CreationComplete of Application?
          ZKVID

          Yep.You can do that.Here is the code

           

          <?xml version="1.0" encoding="utf-8"?>
          <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" creationComplete="func2(),func1()">
             
             
              <mx:Script>
                  <![CDATA[
                     
                      import mx.controls.Alert;
                     
                      private function func1():void
                      {
                     
                          Alert.show("This is Function 1");
                     
                      }
                     
                      private function func2():void
                      {
                     
                          Alert.show("This is Function 2");
                     
                      }
                     
                  ]]>
              </mx:Script>   
             
          </mx:Application>

          1 person found this helpful
          • 2. Re: Can I add multiple function calls in CreationComplete of Application?
            Andrew Rosewarn Level 3

            Hi

             

            Thats works but it would be much clearer to write a function to handle the creation complete and then call the other functions from there.   Especially if there are going to be amore and more functions and other Actionscript blocks.

             

             

            <?xml version="1.0" encoding="utf-8"?>
            <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" creationComplete="creationCompleteHandler()">
                 <mx:Script>
                    <![CDATA[
                       
                        import mx.controls.Alert;
                       
                        private function creationCompleteHandler():void {

                               func1();

                               func2();

                        }

             

                        private function func1():void
                        {
                       
                            Alert.show("This is Function 1");
                       
                        }
                       
                        private function func2():void
                        {
                       
                            Alert.show("This is Function 2");
                       
                        }
                       
                    ]]>
                </mx:Script>   
               
            </mx:Application>

             

             

            Andrew

            1 person found this helpful
            • 3. Re: Can I add multiple function calls in CreationComplete of Application?
              poojagupta Level 1

              The problem still persists.

               

              I have to call two functions in CreationComplete of application. Of these two functions, one is "hts.send()" where "hts" is an xml file. I have inserted the complete code. Please help me make it work.

               

              <?xml version="1.0" encoding="utf-8"?>
              <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" xmlns:Components = "com.hillelcoren.components.*" creationComplete="CreationCompleteHandler()" layout="absolute">
              <mx:HTTPService id="hts" url="assets/trial1.xml" resultFormat="e4x" />
              <mx:XMLListCollection id="xml1" source="{hts.lastResult.Record}" />
              <mx:Script>
               <![CDATA[
                   import mx.controls.Alert;
                import mx.collections.XMLListCollection;
                import com.hillelcoren.components.AutoComplete;
                import com.hillelcoren.components.AdvancedAutoComplete;
                import mx.collections.ArrayCollection;
                   [Bindable]
                public var newArr:ArrayCollection = new ArrayCollection();
                private function CreationCompleteHandler():void {
                 func2();
                 func1();
                }
                private function func2():void {
                 hts.send();
                }
                private function func1(): void { 
                
                 var str:String;
                 for each (var recd:XML in xml1){
                     str = recd.Name.toString();
                              newArr.addItem(str);  
                 }
                   }
               ]]>
              </mx:Script> 
              <Components:AutoComplete id="AC" dataProvider="{newArr}"  x="210" y="154"/>
               <mx:Label x="281" y="235" width="232" id="Label1"/>
                  <mx:Button click="func1()" /> 
              </mx:Application>
              
              • 4. Re: Can I add multiple function calls in CreationComplete of Application?
                sri harsha namala Level 2

                Hi,

                          Specify the ResultEvent and FaultEvent for HttpService,First call the  httpService,

                          if your Service is OK then you will be getting the Result in the ResultHandler.

                          After getting the result In the ResultHanlder assign the Result to Some DataProvider and then call the func1() function..........

                • 5. Re: Can I add multiple function calls in CreationComplete of Application?
                  poojagupta Level 1

                  Thanks for the reply....But can you please explain your answer in greater detail...I am a newbie to Flex.

                   

                  Thanks in advance

                   

                  Pooja

                  • 6. Re: Can I add multiple function calls in CreationComplete of Application?
                    TheFlexGuy

                    I think what Sri was saying is that you're trying to deal with the result data right after calling the function that sends the request.  HTTPService calls take time, so you can't expect to have the result available for the next line of code.  Instead, you addEventListener to hts to listen for a RESULT event.  When in the event handler, you then do the stuff you've coded in func1.  Like this:

                     

                     

                    <?xml version="1.0" encoding="utf-8"?>
                    <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" xmlns:Components = "com.hillelcoren.components.*" creationComplete="CreationCompleteHandler()" layout="absolute">
                    <mx:HTTPService id="hts" url="assets/trial1.xml" resultFormat="e4x" />
                    <mx:XMLListCollection id="xml1" source="{hts.lastResult.Record}" />
                    <mx:Script>
                     <![CDATA[
                         import mx.controls.Alert;
                      import mx.collections.XMLListCollection;
                      import com.hillelcoren.components.AutoComplete;
                      import com.hillelcoren.components.AdvancedAutoComplete;
                      import mx.collections.ArrayCollection;
                      import mx.rpc.events.ResultEvent; // now needed      [Bindable]   public var newArr:ArrayCollection = new ArrayCollection();   private function CreationCompleteHandler():void {    func2();    // func1(); *** no longer call this directly ***   }   private function func2():void {    hts.send();
                       // listen for the result
                       hts.addEventListener(ResultEvent.RESULT, resultHandler);   }   private function resultHandler(event:ResultEvent): void {      var str:String;    for each (var recd:XML in event.result){        str = recd.Name.toString();                 newArr.addItem(str);     }      } ]]> </mx:Script> <Components:AutoComplete id="AC" dataProvider="{newArr}"  x="210" y="154"/> <mx:Label x="281" y="235" width="232" id="Label1"/>     <mx:Button click="func1()" /> </mx:Application>

                     

                    Thats the basic idea.  You'd want a fault event handler to catch fault events in case the file wasn't present, etc.

                    This way, you make sure the result is back before you try to deal with the data.

                     

                    -Marty [ http://www.theflexguy.com ]

                    Helpful/Answered? Please mark it as such.

                    • 7. Re: Can I add multiple function calls in CreationComplete of Application?
                      poojagupta Level 1

                      Wow..thanx a lot...that was really helpful.