11 Replies Latest reply on Aug 19, 2010 10:19 PM by kiran7881

    Flex 3 Passing Data using Custom Componets

    kiran7881 Level 1

      Hi , i am trying to  communicate using Custom Events .
      Means when the user Logins on to a Form .

       

      I am creating a Obkect of my User VO of ActionScript class and

       

      var user:User  = new User();

       

      user.name = UITI.text;
      user.pass = PWDTI.text;
      this.dispatchEvent(new LoginEvent(User));

       

      Now another component is actually listening to this data . Please let me know how can i recive this Logged In data in this component .

       

      Here in thisComponent i am listeing to this data :

       

      <mx:DsiplayComponnet creationComplete = "recivedata()">

       

      public function recivedata():void
      {

       

      this.addEventListener(LoginEvent , datahandling);
      }

       

      public function datahandling():void
      {

       

      // Need logic here as how to recive data
      }

       

      </mx:DsiplayComponnet

       

      Please help me .

       

      Thanks in advnace .

        • 1. Re: Flex 3 Passing Data using Custom Componets
          BhaskerChari Level 4

          Hi Kiran,

           

          The way you dispatching the events and listening is wrong...


          Can you please post your LoginEvent class file...???

           

           

          Thanks,

          Bhasker

          • 2. Re: Flex 3 Passing Data using Custom Componets
            ShardulSingh Level 3

            First create the Login event as below :-

             

            package
            {
                import flash.events.Event;
               
                public class LoginEvent extends Event
                {
                    public static const LOGIN_EVENT:String = "LoginEvent";
                    public var userData : Object;
                   
                    public function LoginEvent(type:String,userData : Object,bubbles:Boolean = false, cancelable:Boolean = false)
                    {
                        super(type,bubbles,cancelable)
                        this.userData = userData;
                    }
                    override public function clone():Event
                    {
                        return new LoginEvent(type,userData, bubbles, cancelable);
                    }

             

                }
            }

             

             

             

             

             


            Now dispatch the event from where you are dispatching. and dont send User instead send
            user.Like this :-

             

            dispatchEvent(new LoginEvent(LoginEvent.LOGIN_EVENT,user);

             


            And where you are listining write function as:-

             

            public function datahandling(event : LoginEvent):void
                {
                  var user : User= event.userData as  User

                 //You will reach here do any thing here .
                }

             


            Hope this will help you.

             

             

             

             

             


            with Regards,
            Shardul Singh Bartwal

            • 3. Re: Flex 3 Passing Data using Custom Componets
              kiran7881 Level 1

              Thank you very much Shardul ,

               

                var user : User= event.userData as  User

               

              But i didn't undersdtand this "event.userData " in the above line . can you please elobrate this .

               

              Thanks in advnace .

              • 4. Re: Flex 3 Passing Data using Custom Componets
                kiran7881 Level 1

                Ya , i understood that userData is a Property of the Event .

                Thanks sir .

                • 5. Re: Flex 3 Passing Data using Custom Componets
                  kiran7881 Level 1

                  Hi , Sorry to reopen this question , as i was unable to pass Data When working CustomObjects . Please see my Entire code and let me know where i am actually doing  wrong .

                   

                  This is my Event :

                   

                  LoginEvent.as

                   

                  Here in this case , i am trying to pass the DTO Object with the help of the Event class

                   


                  package events
                  {
                      import flash.events.Event;
                      import vo.User;
                      public class LoginEvent extends Event
                      {
                          public static const LOGIN_EVENT:String = "LoginEvent";
                          public var user : User;
                         
                          public function LoginEvent(type:String,user:User,bubbles:Boolean = false, cancelable:Boolean = false)
                          {
                              super(type,bubbles,cancelable)
                              this.user = user;
                          }
                          override public function clone():Event
                          {
                              return new LoginEvent(type,user, bubbles, cancelable);
                          }

                   

                      }
                  }

                   

                  This is my VO :

                   

                  package vo
                  {
                      public class User
                      {
                         
                          public var uname:String;
                          public var pass:String;
                         
                          public function User()
                          {
                             
                          }

                      }
                  }

                  This is my MXML FIle where i am actually dispatching the Event and passing the Data in it to another Module which is actually listening to it .

                   

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

                   

                  <mx:Script>
                      <![CDATA[
                          import mx.modules.ModuleLoader;
                          import vo.User;
                          import events.LoginEvent;
                          public function CallMe():void
                          {
                          var user:User = new User();
                              user.uname = UITI.text;
                              user.pass = PWDTI.text;
                              this.dispatchEvent(new LoginEvent(LoginEvent.LOGIN_EVENT,user));
                             
                              var ml:ModuleLoader = new ModuleLoader();
                              ml.url = "/Test.swf";
                             
                          }
                         
                      ]]>
                  </mx:Script>
                     
                      <mx:Form x="183" y="179">
                          <mx:FormItem label="Label">
                              <mx:TextInput  id="UITI" />
                          </mx:FormItem>
                          <mx:FormItem label="Label">
                              <mx:TextInput id="PWDTI"/>
                              <mx:Button label="Button" click="CallMe()"/>
                          </mx:FormItem>
                         
                         
                      </mx:Form>
                     
                  </mx:Application>

                   

                   

                  This is my Module Component , which is actually listening to the LoginEvent .

                   

                  <?xml version="1.0" encoding="utf-8"?>
                  <mx:Module xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" width="400" height="300"  creationComplete="listen()">
                     
                      <mx:Script>
                      <![CDATA[
                          import mx.controls.Alert;
                          import vo.User;
                          import events.LoginEvent;
                              public function listen():void
                              {
                                  Alert("Hai");
                                  this.addEventListener(LoginEvent.LOGIN_EVENT , datahandling);
                              }
                             
                              public function datahandling(event:LoginEvent):void
                              {

                   

                  var user : User= event.user as  User
                  Alert.show(user.pass);
                  Alert.show(user.uname);
                                 
                              }

                   

                         
                      ]]>
                  </mx:Script>

                   

                     
                  </mx:Module>

                   

                   

                   

                   

                  Please help me in solving this thing .

                   

                  Thanks in advance .

                  • 6. Re: Flex 3 Passing Data using Custom Componets
                    BhaskerChari Level 4

                    Hi Kiran,

                     

                    Replace your Application and Module files with the below code and check whether your LoginEvent is being listened now...in Module..

                     

                    Please try this once and let me know....

                     

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


                    <mx:Script>
                        <![CDATA[
                            import mx.modules.ModuleLoader;
                            import vo.User;
                            import events.LoginEvent;
                            public function CallMe():void
                            {
                             var user:User = new User();
                                user.uname = UITI.text;
                                user.pass = PWDTI.text;
                               
                                var ml:ModuleLoader = new ModuleLoader();
                                ml.url = "/Test.swf";           
                                ml.addEventListener("ready", onReady);
                            }
                            public function onReady(event:Event):void
                            {
                       systemManager.dispatchEvent(new LoginEvent(LoginEvent.LOGIN_EVENT,user));        
                         }

                           
                        ]]>
                    </mx:Script>
                       
                        <mx:Form x="183" y="179">
                            <mx:FormItem label="Label">
                                <mx:TextInput  id="UITI" />
                            </mx:FormItem>
                            <mx:FormItem label="Label">
                                <mx:TextInput id="PWDTI"/>
                                <mx:Button label="Button" click="CallMe()"/>
                            </mx:FormItem>
                           
                           
                        </mx:Form>
                       
                    </mx:Application>

                     

                     

                    .........Module File.......

                     

                    <?xml version="1.0" encoding="utf-8"?>
                    <mx:Module xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" width="400" height="300"  preinitialize="listen()">
                       
                        <mx:Script>
                        <![CDATA[
                            import mx.controls.Alert;
                            import vo.User;
                            import events.LoginEvent;
                                public function listen():void
                                {
                                    Alert("Hai");
                                    systemManager.addEventListener(LoginEvent.LOGIN_EVENT , datahandling);
                                }
                               
                                public function datahandling(event:LoginEvent):void
                                {
                                 systemManager.removeEventListener(LoginEvent.LOGIN_EVENT , datahandling);
                        var user : User= event.user as  User
                        Alert.show(user.pass);
                        Alert.show(user.uname);
                                }


                           
                        ]]>
                    </mx:Script>


                       
                    </mx:Module>

                     

                     

                    Thanks,

                    Bhasker Chari

                    • 7. Re: Flex 3 Passing Data using Custom Componets
                      kiran7881 Level 1

                      Thanks Bhaskar for the reply ,

                       

                      I am new to Flex , so i am asking this question , correct me if i am wrong .

                       

                      Is the below line necessary here ??         
                                  ml.addEventListener("ready", onReady);

                       


                      Can't i directly use this line

                       

                      systemManager.dispatchEvent(new LoginEvent(LoginEvent.LOGIN_EVENT,user));    and remove the above line ??

                       

                       

                      Please let me know this . thanks

                      • 8. Re: Flex 3 Passing Data using Custom Componets
                        BhaskerChari Level 4

                        Hi Kiran,

                         

                        Actually the above line is very necessary as you are trying to dispatch the LoginEvent before the eventListener is registered....I mean you are first

                        dispatching the event and then after you are loading the Module.....How do you think the component will listen for that event when your Module component

                        is not yet loaded...

                         

                        So first you need to wait for the Module to be loaded and eventLisner to be registered first before you dispatch the event....Also try to register the

                        eventListener in the preInitialize of your Module as I posted the same in my last post...

                         

                         

                        Hope you got my point and what is the mistake you had done...

                         

                         

                        Thanks,

                        Bhasker

                        • 9. Re: Flex 3 Passing Data using Custom Componets
                          kiran7881 Level 1

                          So first you need to wait for the Module to be loaded and eventLisner to be registered first before you dispatch the event

                           

                           

                           

                          Thanks Bhaskar , the above were lines were very useful .

                           

                          Thanks once again

                          • 10. Re: Flex 3 Passing Data using Custom Componets
                            BhaskerChari Level 4

                            Hi Kiran,

                             

                            Is it working for you now...?? Have you tested it whether the event is being listened in your Module Component..

                             

                             

                            Thanks,

                            Bhasker

                            • 11. Re: Flex 3 Passing Data using Custom Componets
                              kiran7881 Level 1

                              Hi Bhaskar ,

                              Sorry for the late reply as i don't have Flex Builder at my work place .

                              It still didn't worked , i made some changes as required

                               

                              Defined user variable as global (Outside the function of  CallMe()) , added ml.loadModule(); for the above code .

                              I am only pasting the Modified code .

                               

                                 var user:User = new User();

                               

                              public function CallMe():void
                                      {

                               


                                          user.uname = "fdsa"; // Hradcoded currently
                                          user.pass = "cxcds"; //
                              // Hradcoded currently
                                      
                                          var ml:ModuleLoader = new ModuleLoader();
                                          ml.url = "/Test.swf";
                                          ml.addEventListener("ready", onReady);
                                          ml.loadModule();          
                                         
                                      }

                               

                              Used creationComplete inside the ModuleComponnet instead of preinitialize as it is giving me some error .

                               

                              <mx:Module xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" width="400" height="300"  creationComplete="listen()">
                                 
                                  <mx:Script>
                                  <![CDATA[
                                      import mx.controls.Alert;
                                      import vo.User;
                                      import events.LoginEvent;
                                          public function listen():void
                                          {
                                             
                                              systemManager.addEventListener(LoginEvent.LOGIN_EVENT , datahandling);
                                          }
                                         
                                          public function datahandling(event:LoginEvent):void
                                          {
                                             
                                             
                                        var user : User= event.user as  User ;
                                  Alert.show(user.pass.toString());
                                  Alert.show(user.uname.toString());
                                          }


                              While debugging and using Dtrp Over (F6) its going to some unrelated classes ,  such as FocusManager and functions by name  deactivateHandler

                               

                              For example this thing .


                              /**
                                   *  @private  Useful for debugging
                                   */
                                  private function deactivateHandler(event:Event):void
                                  {
                                      var target:InteractiveObject = InteractiveObject(event.target);
                                      // trace("FM " + this + " deactivateHandler ", lastFocus);
                                  }

                               

                               

                              Any help ??