8 Replies Latest reply on Aug 31, 2010 2:47 AM by flex4

    Custom Events not working for me , Please help

    kiran7881 Level 1

      Hi ,

       

      I am trying Passing Data  with Custom Events , but its not working for me . Please help .
      This is my Entire Code :

       

      Basically the code is that i am logging from Application.mxml File and welcome user should be displayed on to another Form (Which is a Custom Componnet )

       

      Please sse the whole code and correct the mistake .

      This is my Event

       

      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)
               {

                  super(type);
      this.user = user;           

              }

       



          }
      }

       

      This is my VO :

       

      package vo
      {
          public class User
          {
             
              public var userName:String;
              public var password:String;
             
              public function User()
              {
              }

       

          }
      }

       

      This is screen where User can Login :

       

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

       


      <mx:Script>
          <![CDATA[
              import mx.controls.Alert;
             
              import events.LoginEvent;
              import vo.User;
              import events.LoginEvent;
             
              public var user:User;
           public     var wel:Welcome ;
              public function  CallMe(event:Event):void
              {
                 
                  Alert.show("Inside Login1");
                  wel = new Welcome();
              user = new User();
              user.userName = UNTI.text;
              user.password =     PWDTI.text;
             
              

       

             
               wel.addEventListener(LoginEvent.LOGIN_EVENT, onReady);

       

              
              }
              public function onReady(event:LoginEvent):void
              {
                  Alert.show("Hai1122"); 
         systemManager.dispatchEvent(new LoginEvent(LoginEvent.LOGIN_EVENT,user));    // Instead of SystemManager i used this keyword also but of no use .     
           }

       


        
          ]]>
      </mx:Script>

       

          <mx:Form x="311" y="201">
              <mx:FormItem label="Label" >
                  <mx:TextInput id="UNTI"/>
              </mx:FormItem>
              <mx:FormItem label="Label" >
                  <mx:TextInput id="PWDTI"/>
                  <mx:Button label="Button" click="CallMe(event)"/>
              </mx:FormItem>
          </mx:Form>
         
      </mx:Application>

       

      This is the Welcome Custom Componnet :

       


      <?xml version="1.0" encoding="utf-8"?>
      <mx:Canvas xmlns:mx="http://www.adobe.com/2006/mxml" width="400" height="300" >
         
          <mx:Form creationComplete="init()">
             
         
          <mx:Script >
              <![CDATA[
                  import vo.User;
                  import events.LoginEvent;
                  import mx.controls.Alert;
                 
                  public function init():void
                  {
                          Alert("Hi");
                       systemManager.addEventListener(LoginEvent.LOGIN_EVENT, datahandling);
                  }
                 
                  public function datahandling(event:LoginEvent):void
                  {
                 
                        var user:User = event.user as User;
                       
                        Alert(user.userName);
                        Alert(user.password)
                  }
                 
              ]]>
          </mx:Script>
          </mx:Form>
      </mx:Canvas>

       

       


      Please help , thanks for reading .

        • 1. Re: Custom Events not working for me , Please help
          Arun Lal

          make bubbles=true in your event Class


          • 2. Re: Custom Events not working for me , Please help
            kiran7881 Level 1

            Hi ,

             

            When i am using SystemManager for dispatching events , as far as my knowledge , the bubbles will not come into picture , Please correct me if i am wrong .

             

            Thanks .

            • 3. Re: Custom Events not working for me , Please help
              flex4 Level 2

              Hi

               

                            Use clone method and override it .. It will return a copy of an object of that event ... what i observed in ur custom event is you are using same

               

               

                            identification for two different objects .. i.e "user"    this.user = user;    

               

                           how can it consider which object is which so avoid using same name in different object or references so use different name for these two values ..

               

               

                              when u initializing the UseVo use one id and for parameter in constructor use another id so that u can have clarification to get it understandable...

               

               

                                  and another thing is u are using canvas as custom component.. What is the use of that canvas as using custom component ..If u want to place some components in canvas then only u will go for custom components .. There is no use of ur custom component here  and all the components here are in application...

               

               

                                            so make things clean and clear .. have a nice day.....

              • 4. Re: Custom Events not working for me , Please help
                kiran7881 Level 1

                Thanks Flex4 , i will follow all your points as you mentioned , but i need some clarification regarding this :

                 


                "when u initializing the UseVo use one id and for parameter in constructor use another id so that u can have clarification to get it understandable... "

                 


                You mean to say use in this way :

                package vo
                {
                    public class User
                    {

                function User()
                {

                }
                       
                        public var userName:String;
                        public var password:String;
                       
                        public function User(uname:String,pwd:String)
                        {
                this.userName = uname;
                this.password = pwd;
                        }

                 

                    }
                }

                Thanks in advnace .

                • 5. Re: Custom Events not working for me , Please help
                  flex4 Level 2

                  Hi Kiran

                   

                                    Here in ur code

                   

                   

                   

                   

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

                   


                  <mx:Script>
                       <![CDATA[
                          import mx.controls.Alert;
                         
                           import events.LoginEvent;
                          import vo.User;
                          import  events.LoginEvent;
                         

                   

                  //add this lines  here

                   

                           public var user:User;
                           public     var wel:Welcome;
                          public var loginEvent:LoginEvent;
                          public function  CallMe(event:Event):void
                          {
                            
                              Alert.show("Inside Login1");
                              wel = new Welcome();
                              user = new User();
                              user.userName = UNTI.text;
                                 user.password =     PWDTI.text;
                                 loginEvent = new LoginEvent(LoginEvent.LOGIN_EVENT,user);
                                 dispatchEvent(loginEvent);
                              this.addEventListener(LoginEvent.LOGIN_EVENT,onReady);
                          }
                         public function onReady(event:LoginEvent):void
                          {
                              Alert.show("Hai1122");
                                 //systemManager.dispatchEvent(new LoginEvent(LoginEvent.LOGIN_EVENT,user));    // Instead of SystemManager i used this keyword also but of no use .    
                       }


                     
                      ]]>
                  </mx:Script>

                   

                       <mx:Form x="311" y="201">
                          <mx:FormItem  label="Label" >
                              <mx:TextInput id="UNTI"/>
                           </mx:FormItem>
                          <mx:FormItem label="Label" >
                               <mx:TextInput id="PWDTI"/>
                              <mx:Button  label="Button" click="CallMe(event)"/>
                           </mx:FormItem>
                      </mx:Form>
                     
                  </mx:Application>

                  • 6. Re: Custom Events not working for me , Please help
                    flex4 Level 2

                    Hi Kiran

                     

                     

                                        I mean what i want to say in ur loginEvent class u are using user as two times for userVO and parameter in the constructor

                     

                                        //In LoginEvent

                     

                                             var user:User;  //here u are using user for User class

                                             public function LoginEvent(type:String, user:User) //here u r using user for User

                                                       {

                                                                 super(type);

                                                                      this.user = user;   // I  mean here in this line so use different name for different classes

                                                       }

                     

                     

                         any way this is not a problem but what i am saying that make code understandable and u must have a clear idea what u want to do in..

                    • 7. Re: Custom Events not working for me , Please help
                      kiran7881 Level 1

                      Thank you very much , one more question , regarding your code :

                       

                      you have added as :

                       

                      this.addEventListener(LoginEvent.LOGIN_EVENT,onReady);

                       

                      Isn't it necesary for doing this :

                       

                      wel = new Welcome();.

                      wel.addEventListener(LoginEvent.LOGIN_EVENT,onReady); (To associate what componnet to add Listener to it)

                       

                      PLease clarify

                       

                      Sorry for troubling you for  each and every piece of code , the problem is that i don't have Flex installed at my work place i place all your suggestions at my home .

                       

                      hope you understand , thanks .

                      • 8. Re: Custom Events not working for me , Please help
                        flex4 Level 2

                        Hi

                         

                                   Kiran here in ur code ur creating one custom component like Welcome.mxml   This is an mxml code and ur initializing this in action script dynamically... Some times it will work But It is not a good programming skills. if u have a custom component Make that available to application easily.

                         

                                   so practice writing code in actionscript.....

                         

                         

                         

                                           And for ur question ..without initializing and firing of any event how can u listen an eventListener.. and here u written one eventListener onReady.. how can that listener will call without u fire that event.. in ur previous code u are not fierd any event like loginEvent.. and here Welcome custom component also no use .. u r not using that component properly ......