13 Replies Latest reply on Sep 21, 2009 3:32 PM by Flex harUI

    addEventListener is not working first time(initial request).

    karnatis

       

      Hi All,

       

       

          It is behaving strangely; I’m having below code in ActionScript and .MXML file.

       

         Very first request it is not displaying inquiry_id in confirmation page but it is displaying this value in later requests. In first request also it is displaying value correctly in getNewInquiryResultHandler in controller but when it comes to newReportSucEventHandler it is displaying null value in BroadModel.inquiryConsumer.inquiry_id

       

       

       

       

      -------------------- NewReportEvent.as(Begin) ---------------------------

       

      package com.broad.events

      {

        import flash.events.Event;

       

        import com.broad.beans.Inquiry;

        import com.broad.beans.InquiryConsumer;

       

        // This custom event should be dispatched if the user

        // successfully logs into the application.

       

        public class NewReportEvent extends Event{   

          public static const REPORT:String = "report";

             

          public function NewReportEvent(){

           

            super(NewReportEvent.REPORT,true,true); //bubble by default     

           

          }

         

          override public function clone():Event{

                  return new NewReportEvent(); // bubbling support inside

            } 

         

        }

      }

      -------------------- NewReportEvent.as(End) ---------------------------

       

      In my controller(action script) I added below code for dispatching this event

      public function NewReportEvent():void

      {

            ro = new RemoteObject();

            setUpAmfChannel();

            ro.destination = "manageInquiryService";

            ro.addEventListener("fault", faultHandler);     

            ro.createInquiry.addEventListener("result", getNewInquiryResultHandler);

            Alert.show("Before addNewInquiry");     

                ro.createInquiry(BroadModel.inquiryConsumer);

      }//End of NewReportEvent

       

       

      public function getNewInquiryResultHandler(event:ResultEvent):void

      {

           

            BroadModel.inquiryConsumer.inquiry_id = event.result as String;     

            Alert.show("getNewInquiryResultHandler:"+BroadModel.inquiryConsumer.inquiry_id);

           

      }//End of getNewInquiryResultHandler

       

      In Report.mxml file is having following code

       

      ---------------Report.mxml (Begin) ---------------

       

      <?xml version="1.0" encoding="utf-8"?>

      <mx:Canvas xmlns:mx="http://www.adobe.com/2006/mxml" width="100%" height="100%">

           

           

      <mx:Script>

            <![CDATA[

            import mx.controls.Alert;

            import com.broad.controller.BroadController;

            import com.broad.model.BroadModel;

            import com.broad.events.*;   

           

            [Bindable]

            public var mainLableText:String="NEW REPORT";

            [Bindable]

            public var titleText:String="Create A New Report";

           

       

           

          public function init():void{

            Alert.show("Inside newreport init mxml");

           

            myViewStack.selectedChild = newReport;

            Confirmation.visible=false;       

            broad.selectedItem.label = "Yes";

            // Reset the value too.

            first_name.text = "";       

            last_name.text = "";

            street_address.text = "";

            street_address2.text = "";

            city.text = "";

            state.text = "";

            zip.text = "";         

           

          }

         

          private function initHandler():void

          {

            Alert.show("Inside initHandler");

            addEventListener(NewReportEvent.REPORT,newReportSucEventHandler);

          }

         

       

            private function newReportSucEventHandler(event:NewReportEvent):void{ 

                 

                  Alert.show("Inside newReportEventHandler mxml");

                  myViewStack.selectedChild = Confirmation;

                  newReport.visible=false;

                 

                  Alert.show("Inside newReportEventHandler inquiryId:"+BroadModel.inquiryConsumer.inquiry_id);

                 

                  inquiryIdVal.text = BroadModel.inquiryConsumer.inquiry_id;

            } 

         

         

           

                 

                  public function submitInquiry(evt:Event):void{

                 

                  BroadModel.inquiryConsumer.first_name = first_name.text;

                  BroadModel.inquiryConsumer.last_name = last_name.text;

                  BroadModel.inquiryConsumer.street_address = street_address.text;

                  BroadModel.inquiryConsumer.street_address2 = street_address2.text;

                  BroadModel.inquiryConsumer.city = city.text;

                  BroadModel.inquiryConsumer.state = state.text;

                  BroadModel.inquiryConsumer.zip = zip.text;

                 

                  Alert.show("Inside submitInquiry state:",currentStateVal);

                 

                  this.dispatchEvent(new NewReportEvent());     

                 

                  }         

                 

            ]]>

      </mx:Script>

       

       

       

       

            <mx:ViewStack id="myViewStack" width="100%" height="100%" creationPolicy="all" >

                 

            <mx:Canvas id="newReport" height="100%" width="100%" visible="true">

            <mx:Label x="10" y="10" text="{mainLableText}" fontSize="18" fontWeight="bold" color="#F07012"/>

                 

            <mx:LinkButton id="viewEditLink" right="10"  styleName="htmlLink" label="{pageMode}" click="setPageMode(event);"

                  visible="{pageMode != null}" includeInLayout="{pageMode != null}" />

                                                           

            <mx:TitleWindow width="100%" height="100%" layout="absolute" title="{titleText}" fontWeight="normal" fontSize="13" y="38" x="0">

            <mx:Canvas height="100%" width="100%">

            <mx:VBox width="100%" height="100%">

                <mx:HBox>

                      <mx:Label text="Consumer Information: " fontSize="12" fontWeight="bold" color="#34B05D"/>

                </mx:HBox>

               

                <mx:HBox>

                      <mx:Label text="Report Type: " fontWeight="normal"/>

                      <mx:Label text="Phone" fontWeight="bold"/>

                </mx:HBox>

               

               

                <mx:HBox width="100%">

                      <mx:Label width="25%"  text="First Name:" fontWeight="normal"/>

                      <mx:TextInput width="25%" id="first_name" editable="{pageModeStat}" />

                      <mx:Label  width="25%" text="Last Name:" fontWeight="normal"/>

                      <mx:TextInput width="25%" id="last_name"/>

                </mx:HBox>

               

                <mx:HBox width="100%">

                      <mx:Label  width="25%" text="Address1: " fontWeight="normal"/>

                      <mx:TextInput width="25%" id="street_address"/>

                      <mx:Label  width="25%" text="Address2:" fontWeight="normal"/>

                      <mx:TextInput width="25%" id="street_address2"/>

                </mx:HBox>

               

                <mx:HBox width="100%">

                      <mx:Label  width="25%" text="City: " fontWeight="normal"/>

                      <mx:TextInput width="25%" id="city"/>

                      <mx:Label  width="25%" text="State:" fontWeight="normal"/>

                      <mx:TextInput width="25%" id="state"/>

                </mx:HBox>

               

                <mx:HBox width="100%">

                      <mx:Label  width="25%" text="Zipcode: " fontWeight="normal"/>

                      <mx:TextInput width="25%" id="zip"/>

                      <mx:Label  width="25%" text="Phone"/>

                      <mx:TextInput width="25%" id="phone"/>

                </mx:HBox>

                         

                   

                <mx:HBox width="100%" horizontalAlign="center">

                      <mx:Button label="SUBMIT REPORT" id="submit_search" click="submitInquiry(event)" fillColors="#34B05D"/>

                </mx:HBox>     

                 

                  </mx:VBox>

            </mx:Canvas>

                 

            </mx:TitleWindow>

           

            </mx:Canvas>

           

           

           

                  <mx:Canvas id="Confirmation" width="100%" height="100%" creationComplete="initHandler()">

           

                  <mx:Label x="10" y="10" text="NEW REPORT"  fontSize="18" fontWeight="bold" color="#F07012"/>

                  <mx:TitleWindow width="100%" height="132" layout="absolute" title="Create A New Report" fontWeight="normal" fontSize="13" y="38" x="0">

                  <mx:VBox width="100%" height="81">

                      <mx:HBox>

                            <mx:Label text="Submission Confirmation:" fontSize="12" fontWeight="bold" color="#34B05D"/>

                      </mx:HBox>

                     

                      <mx:HBox>

                            <mx:Label text="Report has been created successfully with Report ID:"/>

                            <mx:Label id="inquiryIdVal" fontSize="12" fontWeight="bold" color="#34B05D"/>

                      </mx:HBox>

                  </mx:VBox>

                           

                     

                  </mx:TitleWindow>

                 

                  </mx:Canvas>

            </mx:ViewStack>

       

      </mx:Canvas>

       

      ---------------Report.mxml (End) ---------------

       

         Please check this where I'm doing wrong, it is displaying inquiry_id correctly in controller but it is not getting this value in Report.mxml file for first time request. Laster requests I'm getting inquiry_id value.

       

         Can someone please let me know where I'm doing worng?

       

         Thanks in advance.

       

      Regards,

      Sharath Karnati.

       

       

       

       

        • 1. Re: addEventListener is not working first time(initial request).
          karnatis Level 1

          I’m really stuck, can someone please let me know what changes I need to make so that it works for every request. Now I’m having problem for very first request. I think this could be something initialization problem? I couldn’t able to figure out

           

          Thanks,

          Sharath.

          • 2. Re: addEventListener is not working first time(initial request).
            Flex harUI Adobe Employee

            Put a breakpoint or trace statement on the addEventListener call and the dispatchEvent call and see which gets called first.

             

            Alex Harui

            Flex SDK Developer

            Adobe Systems Inc.

            Blog: http://blogs.adobe.com/aharui

            • 3. Re: addEventListener is not working first time(initial request).
              karnatis Level 1

              Please find application logic flow:

               

              1. In 'Confirmation' canvas we are added creationComplete="initHandler()"

               

                  initHandler() method is having following code

               

                  private function initHandler():void
                  {
                      Alert.show("Inside initHandler");      
                      addEventListener(NewReportEvent.REPORT,newReportSucEventHandler);
                  }

               

              2. It is invoking newReportSucEventHandler() method, which is having below code

               

                  private function newReportSucEventHandler(event:NewReportEvent):void{   
                     
                      myViewStack.selectedChild = Confirmation;
                      newReport.visible=false;       
                     
                      Alert.show("Inside newReportEventHandler inquiryId:"+BroadbandModel.inquiryConsumer.inquiry_id);
                     
                      inquiryIdVal.text = BroadbandModel.inquiryConsumer.inquiry_id;
                  }

               

              3. In submit we are invoking below code

               

                public function submitInquiry(evt:Event):void{     

                     
                      BroadModel.inquiryConsumer.first_name = first_name.text;
                      BroadModel.inquiryConsumer.last_name = last_name.text;
                      BroadModel.inquiryConsumer.street_address = street_address.text;
                      BroadModel.inquiryConsumer.street_address2 = street_address2.text;
                      BroadModel.inquiryConsumer.city = city.text;
                      BroadModel.inquiryConsumer.state = state.text;
                      BroadModel.inquiryConsumer.zip = zip.text;
                            
                      this.dispatchEvent(new NewReportEvent());      
                     
                      }

               

               

                I think the order of execution of dispatchEvent and addEventListener is wrong, can you please let me know how to invoke addEventListener after calling dispathEvent.

               

                I'm in a impression that when we create addEventListener then it will be called by flex once it completes process in dispatchEvent.

               

                Thanks in advance.

               

              Regards,

              Sharath.

              • 4. Re: addEventListener is not working first time(initial request).
                OzzyOGRicca

                It's probably because you're naming your function the same as the class name. Try renaming the function to 'dispatchNewReportEvent()'

                • 5. Re: addEventListener is not working first time(initial request).
                  Flex harUI Adobe Employee

                  Calling addEventListener should not be resulting in a call to newRepertSucEventHandler unless later code dispatches the NewReportEvent.REPORT event so I don't know how you get to step 2.

                   

                  Also note that this:

                         

                  • this.dispatchEvent(new NewReportEvent());*      

                  Might need to be this:

                         

                  • this.dispatchEvent(new NewReportEvent(NewReportEvent.REPORT));*      

                   

                  Also make sure the 'this' pointer is correct for the addEventListener and dispatchEvent calls

                   

                  Alex Harui

                  Flex SDK Developer

                  Adobe Systems Inc.

                  Blog: http://blogs.adobe.com/aharui

                  • 6. Re: addEventListener is not working first time(initial request).
                    karnatis Level 1

                    Previously, when I'm calling below line in Report.mxml file

                     

                    this.dispatchEvent(new NewReportEvent());

                     

                    In Controller, I'm having following line in init() method

                     

                    private function init( event:Event ):void{
                          systemManager.addEventListener(LoginEvent.LOGIN, login, true);
                          systemManager.addEventListener(LoginSuccessEvent.LOGIN_SUCCESS, handleLogin, true);
                          systemManager.addEventListener(LoginFailureEvent.LOGIN_FAILURE, handleLogin, true);
                          systemManager.addEventListener(ValidateLoginEvent.LOGIN_VALIDATE, checkLogin, true);
                          systemManager.addEventListener(HomeEvent.HOME, home, true);
                          systemManager.addEventListener(SearchEvent.SEARCH, search, true);
                          systemManager.addEventListener(NewReportEvent.REPORT, newInquiry, true);
                          login(new LoginEvent(LoginEvent.LOGIN));
                        }

                     

                    public function newInquiry(evt:NewReportEvent):void{
                            Alert.show("Inside newInquiry", evt.type);
                            addNewInquiry();  
                           
                        }//End of newInquiry

                     

                    public function addNewInquiry():void
                        {
                            ro = new RemoteObject();
                            setUpAmfChannel();
                            ro.destination = "manageInquiryService";
                            ro.addEventListener("fault", faultHandler);      
                            ro.createInquiry.addEventListener("result", getNewInquiryResultHandler);
                            Alert.show("Before addNewInquiry");      
                            ro.createInquiry(BroadModel.inquiryConsumer);
                           
                        }//End of addNewInquiry

                     

                    public function getNewInquiryResultHandler(event:ResultEvent):void
                        {
                             
                           BroadModel.inquiryConsumer.inquiry_id = event.result as String;      
                           Alert.show("getNewInquiryResultHandler:"+BroadModel.inquiryConsumer.inquiry_id);

                             
                        }//End of getNewInquiryResultHandler

                     

                     

                    Did following changes

                     

                    In Controller:

                        // systemManager.addEventListener(NewReportEvent.REPORT, newInquiry, true);  COMMENTED

                     

                    Report.mxml:

                     

                         public function submitInquiry(evt:Event):void{

                     

                            var controller:BroadController = new BroadController();
                            controller.addNewInquiry();
                           
                              this.dispatchEvent(new NewReportEvent(NewReportEvent.REPORT));

                        }

                     

                        Still I'm having same issue, it is calling this.addEventListener(NewReportEvent.REPORT,newReportSucEventHandler); before dispathEvent and it is showing null value in confirmation page.

                     

                        Can you please check this code and let me know what I'm doing wrong. Why addEventListerner is firing before dispatchEvent?

                     

                    Thanks,

                    Sharath.

                    • 7. Re: addEventListener is not working first time(initial request).
                      Flex harUI Adobe Employee

                      I can only spend a few seconds looking at code in the forums.  It is best if you use the debugger to follow your code and verify your logic.  I don't know why you added a capture phase listener.  Because there is no capture phase at the target, it could cause you to miss the event.

                       

                      Normally you do want to call addEventLIstener before calling dispatchEvent so I don't know why you think that is wrong.

                       

                      Alex Harui

                      Flex SDK Developer

                      Adobe Systems Inc.

                      Blog: http://blogs.adobe.com/aharui

                      • 8. Re: addEventListener is not working first time(initial request).
                        karnatis Level 1

                        Hi Alex,

                         

                            I'm submitting Consumer information and it is stored in database and it will return inquiry_id. The returned inquiry_id, I'd like to show in confirmation page.

                         

                           The problem I'm phasing is, there will some time dealy for getting inquiry_id from server before that it is displaying confirmation page without inquiry_id because of that I'm trying to implement dispatchEvent and addEventListeners, I thought that after completing dispatchEvent method logic it will invoke addEventListerner method.

                         

                           In case, if I hardcode value inquiry_id in controller then functionality is working fine. How to achieve this  using FLEX.

                         

                           Thanks in advance.

                         

                        Regards,

                        Sharath.

                        • 9. Re: addEventListener is not working first time(initial request).
                          Flex harUI Adobe Employee

                          Don't show the confirmation page until you get the RESULT event from the server.

                           

                          Alex Harui

                          Flex SDK Developer

                          Adobe Systems Inc.

                          Blog: http://blogs.adobe.com/aharui

                          • 10. Re: addEventListener is not working first time(initial request).
                            karnatis Level 1

                            Hi Alex,

                             

                            In controller I'm having following handler for ResultEvent

                             

                            public function getNewInquiryResultHandler(event:ResultEvent):void
                                {
                                     
                                   BroadModel.inquiryConsumer.inquiry_id = event.result as String;      
                                   Alert.show("getNewInquiryResultHandler:"+BroadModel.inquiryConsumer.inquiry_id) ;

                                     
                                }//End of getNewInquiryResultHandler

                             

                             

                            Can you please let me know what code I need to add to display cofirmation page in controller. I tried creating instance of Report.mxml file but unable to display confirmation page.

                             

                            Thanks,

                            Sharath.

                            • 11. Re: addEventListener is not working first time(initial request).
                              Flex harUI Adobe Employee

                              There are many ways. I would suggest learning how to use "states" to control what is on screen.

                               

                              Alex Harui

                              Flex SDK Developer

                              Adobe Systems Inc.

                              Blog: http://blogs.adobe.com/aharui

                              • 12. Re: addEventListener is not working first time(initial request).
                                karnatis Level 1

                                Hi Alex,

                                 

                                   Modified controller(.as) file as below

                                 

                                    public function getNewInquiryResultHandler(event:ResultEvent):void
                                    {
                                         
                                       BroadModel.inquiryConsumer.inquiry_id = event.result as String;      
                                       Alert.show("getNewInquiryResultHandler:"+BroadModel.inquiryConsumer.inquiry_id) ;

                                 

                                        var newReport:NewReport = new NewReport();

                                        addChild(newReport);
                                       newReport.confirmation();

                                         
                                    }//End of getNewInquiryResultHandler

                                 

                                 

                                   In Report.mxml file added confirmation() method, which will change confirmation state(myViewStack)

                                 

                                    public function confirmation():void{   
                                       
                                        Alert.show("Inside confirmation mxml");    
                                        myViewStack.selectedChild = Confirmation;
                                        newReport.visible=false;     
                                       
                                        Alert.show("Inside newReportEventHandler inquiryId:"+BroadbandModel.inquiryConsumer.inquiry_id);
                                       
                                        inquiryIdVal.text = BroadbandModel.inquiryConsumer.inquiry_id;
                                    }

                                 

                                   When I run application, It is calling confirmation() method but it is not displaying confirmation state(viewstack). It is still displaying 'newReport' canvas. Can you please let me know, what else I need to change to display confirmation in Controller(.AS) code or confirmation() method in .MXML file.

                                 

                                Thanks,

                                Sharath.

                                • 13. Re: addEventListener is not working first time(initial request).
                                  Flex harUI Adobe Employee

                                  It is definitely time for you to learn to use the debugger.  It is much more efficient than my trying to be a compiler and debugger in my head.

                                   

                                  Alex Harui

                                  Flex SDK Developer

                                  Adobe Systems Inc.

                                  Blog: http://blogs.adobe.com/aharui