16 Replies Latest reply on Sep 1, 2009 10:37 AM by DeyBwah

    Newbie Question (DispatchEvent is not reaching Main Application)

    DeyBwah

      Hi everyone,

       

      I'm trying to dispatch an event from within an mxml component(CompB) to interact with another mxml component(CompA) and they both live on the Main Application(addChild), yet it doesn't seem to be reaching the Main Application.

       

      There are no errors thrown either...

       

      I feel like it may be a syntactical error.

       

      Any direction would be greatly appreciated!

       

      Here is my code... (condensed to show only what is needed)

       

      CompB:

       

      <mx:Panel xmlns:mx="http://www.adobe.com/2006/mxml"

           creationComplete="creationCompleteHandler()">

       

           <mx:Script>

                <![CDATA[

       

                     private function creationCompleteHandler():void {

                          videoDisplay.addEventListener(VideoEvent.COMPLETE, videoCompleteHandler);

                     }

       

                     public function videoCompleteHandler(event:VideoEvent):void {

                          dispatchEvent(new Event('videoComplete'));

                     }

                ]]>

           </mx:Script>

       

      </mx:Panel>

       

      ------------------------------------------------------------------------------------------ -----

       

       

      Main Application:

       

      <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"

           initialize="init()">

       

           <mx:Script>

                <![CDATA[

                     import com.CompA;

                     import com.CompB;              

       

                     private var myFancyCompA:CompA;

                     private var myFancyCompB:CompB

       

                     private function init():void {

                          myFancyCompB.addEventListener('videoComplete', completeHandler);

                          addChild(myFancyCompB);

                     }

       

                     public function completeHandler(event:Event):void {

                          Alert.show("Success!");

                          myFancyCompB.visible = false;

                     }

                ]]>

           </mx:Script>

       

      </mx:Application>

       

      When I run the application and the video ends, no alert pop-up appears...

       

      I've narrowed it down to either the dispatch or listener.

       

      I feel like the event isn't being dispatched correctly.

       

      Thanks!!

      DK

        • 1. Re: Newbie Question (DispatchEvent is not reaching Main Application)
          babo_ya Level 3

          try double quote...

           

          dispatchEvent(new Event("videoComplete"));

           

           

          myFancyCompB.addEventListener("videoComplete", completeHandler);

           

          hope this helps,

          1 person found this helpful
          • 2. Re: Newbie Question (DispatchEvent is not reaching Main Application)
            DeyBwah Level 1

            Anyoung Ha Say Yo!

             

            I'm not at the office, but will try that first thing Monday morning, thanks for the suggestion!

             

            I'll report back to this forum on the details.

             

            Namaste,

            DK

            • 3. Re: Newbie Question (DispatchEvent is not reaching Main Application)
              RUSH-ME Level 3

              Hi

               

              Your myFancyCompB is not initialized,. you might not have debug version of your flash player.  So first initialize it like

               

              private var myFancyCompB:CompB = new compB();

               

              Hope this helps.

               

              Regards

              Rush-me

              1 person found this helpful
              • 4. Re: Newbie Question (DispatchEvent is not reaching Main Application)
                DeyBwah Level 1

                Hi Rush,

                 

                Thanks for the response,

                 

                I actually did initialize it, just didn't post that bit of code... probably should have, I tried to condense the code as much as possible and left out quite a bit of the extraneous code.

                 

                Should have included that code in the post I'm realizing after the fact.

                 

                I'm mainly concerned that my dispatchEvent or listener code is incorrect.

                 

                I do have Flash Debug, and no errors/problems were detected..

                 

                Namaste,

                Dey

                • 5. Re: Newbie Question (DispatchEvent is not reaching Main Application)
                  Andrew Rosewarn Level 3

                  Hi Dey Kim

                   

                  I put your sample together in my Flex Builder and this seems to be working.

                   

                  Application.

                   

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

                  <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" initialize="init()" xmlns:local="*">

                   

                       <mx:Script>

                            <![CDATA[

                                import mx.controls.Alert;             

                   

                                private var myFancyCompB:CompB;

                   

                   

                                private function init():void {

                                      myFancyCompB = new CompB;                  

                                      myFancyCompB.addEventListener('videoComplete', completeHandler);

                                      addChild(myFancyCompB);

                                 }

                   

                                 public function completeHandler(event:Event):void {

                                      Alert.show("Success!");

                                      myFancyCompB.visible = false;

                                 }

                            ]]>

                       </mx:Script>

                  </mx:Application>

                   

                  Comp B

                   

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

                  <mx:Panel xmlns:mx="http://www.adobe.com/2006/mxml"

                       creationComplete="creationCompleteHandler()">

                   

                       <mx:Script>

                            <![CDATA[

                            import mx.events.VideoEvent;

                   

                                 private function creationCompleteHandler():void {

                                      videoDisplay.addEventListener(VideoEvent.COMPLETE, videoCompleteHandler);

                                 }

                   

                                 public function videoCompleteHandler(event:VideoEvent):void {

                                      dispatchEvent(new Event('videoComplete'));

                                 }

                            ]]>

                       </mx:Script>

                       <mx:VideoDisplay id="videoDisplay" width="92" height="94" source="file:////Users/Andrew/Desktop/barsandtone.flv"/>

                   

                  </mx:Panel>

                   

                  I couldn't see you instansiate  compB anywhere, you should have myCompB = new myCompB, but then I would have thought flexbuilder would have thrown an error when you tried to add the event listener.

                   

                  Anyway this works have a look.

                   

                  Andrew

                   

                  • 6. Re: Newbie Question (DispatchEvent is not reaching Main Application)
                    christopher.mina Level 1

                    Ditto that.  It worked fine for me as well.  THe only thing we didn't see was where / how you added the video player to the stage.  I'm assuming it was in the panel in mxml.  If that is the case, you can also always use mxml to listen for the "complete" event, instead of relying on CreationComplete of the panel to add the VideoEvent.COMPLETE listener:

                     

                    <mx:VideoDisplay id="vid" complete="onVideoComplete(event)"/>

                    • 7. Re: Newbie Question (DispatchEvent is not reaching Main Application)
                      Andrew Rosewarn Level 3

                      Yea very true, I used mxml in my sample but added the listener with Actionscript.  Also just watch that;

                       

                      1. You have a source specified for the videoDisplay Component

                       

                      and

                       

                      2. That its a valid url or path and that the video plays.  If it can't find the file it will just sit there, no error message, and therefore the videoComplete event will never get fired.

                       

                      Apart from that its worked for two of us so you should be good to go.

                       

                      Andrew

                      • 8. Re: Newbie Question (DispatchEvent is not reaching Main Application)
                        DeyBwah Level 1

                        Hi Andrew,

                         

                        Thanks again for taking the time to "school" me!

                         

                        I don't have access to my code (it's at work) but I will definitely pull out my magnifying glass and see what I discover first thing Monday morning, reporting the results of course.

                         

                        Hi Chris,

                         

                        Most of the code was created by a former employee who had a much more advanced skillset with Flex and OOP, I on the other hand am just starting out (and loving it of course) with Flex, AS3 AND OOP... It has been quite overwhelming but fun at the same time, you could say I've been bitten by the Flex Bug and now a full-fledged believer!

                         

                        I don't think the video player is being pulled in via MXML, but instead, AS. Thanks for your input!

                         

                        Namaste,

                        Dey

                        • 9. Re: Newbie Question (DispatchEvent is not reaching Main Application)
                          DeyBwah Level 1

                          Hi everyone,

                           

                          So I took a look at the sample posted by Andrew and still am having trouble to get the dispatched event to execute...

                           

                          Here is the non-condensed code. The videoPlayer is being pulled in via an AS function that gets called through a HTTPService result that calls the function videoHandler.

                           

                          Main Application:

                           

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

                          <mx:Application

                               xmlns:pvp="com.PVideoPlayer.*"

                               xmlns:mx="http://www.adobe.com/2006/mxml"

                               layout="absolute"

                               initialize="init()"

                               backgroundColor="0x000000">

                           

                           

                               <mx:Style source="main_style.css"/>

                           

                               <mx:Script>

                                    <![CDATA[

                                         import com.SharingBookmarks;

                                         import com.PVideoPlayer.PVideoPlayer;

                                         import flash.external.*;

                                         import mx.rpc.events.ResultEvent;

                                         import mx.rpc.events.FaultEvent;

                                         import mx.controls.Alert;

                           

                           

                                         [Bindable]

                                         private var referer:String;

                                         [Bindable]

                                         private var clipID:String;

                                         [Bindable]

                                         private var v_source:String;

                                         [Bindable]

                                         private var v_fullScreenMode:Boolean;

                                         [Bindable]

                                         private var v_autoPlay:Boolean;

                                         [Bindable]

                                         private var v_clipTag:String;

                                         [Bindable]

                                         private var v_iag_code:String;

                                         [Bindable]

                                         private var v_enable:Boolean;

                                         [Bindable]

                                         private var v_officialURL:String;

                                         [Bindable]

                                         private var v_gID:String;

                                         [Bindable]

                                         private var v_starterImageURL:String;

                           

                            

                                         private var sharingBookmarks:SharingBookmarks;

                                         private var videoPlayer:PVideoPlayer;

                                         private var alert:Alert;

                           

                                      

                                         private function init():void {

                                              trace('init');

                                              referer = getReferer();

                                              clipID = getClipID();

                           

                                            

                                              if (clipID != '') {

                                                   trace('videoByMovieRPC.send');

                                                   videoByMovieRPC.send();

                                              }

                                            

                                              sharingBookmarks = new SharingBookmarks();

                                              sharingBookmarks.addEventListener('clicked', handleClick);

                                              sharingBookmarks.addEventListener('videoComplete', sharingVisible);

                                              addChild(sharingBookmarks);

                                              sharingBookmarks.visible = false;  

                                         }

                           

                           

                                         public function handleClick(event:Event):void {

                                              videoPlayer.showVideo();

                                         }

                           

                           

                                         public function sharingVisible(event:Event):void {

                                              sharingBookmarks.visible = true;

                                              Alert.show("Your email has been sent successfully!");

                                         }

                           

                           

                                         private function videoHandler(event:ResultEvent,id:String):void{

                                              trace('videoHandler');

                                              var videoXML:XMLList = event.result.movie.video.(@videoID==id) as XMLList;

                           

                                            

                                              if (videoXML.length() > 0) {

                           

                                                   v_source = videoXML.source;

                                                   v_fullScreenMode = strToBool(videoXML.fullScreenMode);

                                                   v_autoPlay = strToBool(videoXML.autoPlay);

                                                   v_clipTag = videoXML.clipTag;

                                                   v_iag_code = videoXML.iag_code;

                                                   v_enable = strToBool(videoXML.enable);

                                                   v_officialURL = videoXML.parent().@officialURL;

                                                   v_gID = videoXML.parent().@gID;

                                                   v_starterImageURL = videoXML.starterImageURL;

                           

                                                

                                                   videoPlayer = new PVideoPlayer();

                                                   videoPlayer.source = v_source;

                                                   videoPlayer.fullScreenMode = v_fullScreenMode;

                                                   videoPlayer.autoPlay = v_autoPlay;

                                                   videoPlayer.clipTag = v_clipTag;

                                                   videoPlayer.iag_code = v_iag_code;

                                                   videoPlayer.enabled = v_enable;

                                                   videoPlayer.officialURL = v_officialURL;

                                                   videoPlayer.referer = referer;

                                                   videoPlayer.gID = v_gID;

                           

                           

                                              }

                           

                                              if (v_starterImageURL != '')

                                                   videoPlayer.starterImageURL = v_starterImageURL;

                           

                                                   trace('videoHandler:addChild(videoPlayer)');

                                                   addChild(videoPlayer);

                                              }

                                         }

                           

                           

                                         private function getReferer():String {

                                              var result:String = '';

                                                

                                              if (this.parameters.ws == 'true'){ // Safarai on Windows

                                                   result = getDomain(); // JS call

                                              } else {

                                                   result = this.parameters.r; // GET variable from URL

                                              }

                           

                           

                                              if ((result == null) || (result == '')){

                                                   result = 'unknown';

                                              }

                           

                                              trace('getReferer:'+result);

                                              return result;

                                         }

                           

                           

                                         private function getClipID():String {

                                              var result:String = '';

                           

                                                

                                              if (this.parameters.cid != null) {

                                                   result = this.parameters.cid;

                                              }

                                                

                                              trace('getClipID:'+result);

                                              return result;

                                         }

                           

                           

                                         private function getDomain():String {

                                              var url:String = '';

                                              var domain:String = '';

                           

                                              url = ExternalInterface.call("window.location.href.toString");

                                              domain = url.substring(url.indexOf("://")+3,url.indexOf("/",8));

                           

                                              trace('getDomain:'+domain);

                                              return domain;

                                        }

                           

                           

                                         private function httpService_fault(evt:FaultEvent):void {

                                             trace('httpService_fault');

                                             alert = Alert.show('Sorry, there is a connection problem at this moment, please try again later.');

                                             alert.addEventListener(MouseEvent.MOUSE_DOWN, alert_mouseDown, true);

                                             v_enable = false;

                                         }

                           

                                         private function alert_mouseDown(evt:MouseEvent):void {

                                              evt.stopImmediatePropagation();

                                         }

                           

                           

                                         private function strToBool(string:String):Boolean{

                                              return (string == 'true');

                                         }

                                   }

                               ]]>

                               </mx:Script>

                           

                           

                               <mx:HTTPService

                                    id="videoByMovieRPC"

                                    url="http://www.soandso.com/jazzmaster/catalog.xml"

                                    result="videoHandler(event,clipID)"

                                    fault="httpService_fault(event);"

                                    resultFormat="e4x"/>

                           

                          </mx:Application>

                           

                           

                          PVideoPlayer:

                           

                           

                          private function creationCompleteHandler():void {

                               trace('creationCompleteHandler');

                              videoDisplay.addEventListener(VideoEvent.COMPLETE, videoCompleteHandler);

                          }

                           

                           

                          public function videoCompleteHandler(event:VideoEvent):void {

                               dispatchEvent(new Event("videoComplete"));

                               hideVideo();

                          }

                           

                           

                          public function hideVideo():void {

                                 this.visible = false;

                                 this.includeInLayout = false;

                          }

                           

                           

                          CompB in the previous posts are PVideoPlayer, CompA is SharingBookmarks.

                           

                          The purpose is to get the sharingBookmarks to be hidden until it is needed, when the videoPlayer is done playing the clip, it gets hidden via visible = false. At that point, I'm trying to get the sharingBookmarks visible to = true.

                           

                          Seems like all is working minus the sharingBookmarks becoming visible which is what is leading me to conclude that the dispatchEvent is either not working properly or the listener isn't catching the dispatched event...

                           

                          Thanks for looking!

                          DK                                                                                                           

                          • 10. Re: Newbie Question (DispatchEvent is not reaching Main Application)
                            DeyBwah Level 1

                            Hi everyone, found the issue...

                             

                            For some reason, the absense of the second parameter in my dispatchEvent was preventing the dispatchEvent from bubbling to the Main Application..

                             

                            public function videoCompleteHandler(event:VideoEvent):void {

                              dispatchEvent(new Event('videoComplete', true));

                            }

                             

                            Setting the second parameter to true actually fixed my issue and the dispatched event is now reaching the main application and running the eventHandler.

                             

                            Anyone know why it was working for Andrew and Chris, as well as for another dispatchEvent in a different part of my application without that second parameter??

                             

                            Although I'm happy that my application is now working, I would still like to know in which cases you need to set that second parameter to enable dispatchEvents to bubble and which cases you don't.

                             

                            Thanks all!

                            DK

                            • 11. Re: Newbie Question (DispatchEvent is not reaching Main Application)
                              Flex harUI Adobe Employee

                              Events do not bubble by default, because bubbling is not a recommended practice.

                               

                              Alex Harui

                              Flex SDK Developer

                              Adobe Systems Inc.

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

                              • 12. Re: Newbie Question (DispatchEvent is not reaching Main Application)
                                DeyBwah Level 1

                                Hi HarUI,

                                 

                                Can you elaborate further on that? The reason I'm asking for more clarification is that one of my dispatched events are successfully making it to the main application without setting the bubbling parameter to "true". Why is that?

                                 

                                Thanks,

                                DK

                                • 13. Re: Newbie Question (DispatchEvent is not reaching Main Application)
                                  Andrew Rosewarn Level 3

                                  Your event dispatching component sends the event up the hierachy of the application.  If the parent is the application it will hear it.  If your component is buried a bit further down bubbling (set to true) allows it to travel up the hierachy until it reached the top level.  You can stop it bubbling at certain points using stopPropogation() or stopImmediatePropogation().

                                   

                                  However can I further add to thid question to HarUI, or anyone else and ask why is bubbling considered bad practice.  I've seen other apps do it.  If you wanted to send an event up two levels so to speak, would you therefore listen and re dispatch at each level.

                                   

                                  Whats the best practice for this?


                                  Many Thanks.

                                  • 14. Re: Newbie Question (DispatchEvent is not reaching Main Application)
                                    christopher.mina Level 1

                                    Please read the docs section on Event propogation.  It should answer all of your questions, plus some:

                                     

                                    http://livedocs.adobe.com/flex/3/html/help.html?content=events_08.html

                                     

                                    Some events do have bubbling enabled by default and this could be why you're noticing one event reaching its target while another is not.  It could also have to do with how you're defining your event listeners, whether or not you are using the capture phase in your listener. 

                                     

                                    -Chris

                                    • 15. Re: Newbie Question (DispatchEvent is not reaching Main Application)
                                      DeyBwah Level 1

                                      @Andrew,

                                       

                                      Your response answered my question and your question is also of great interest to me (why bubbling might be considered bad practice).

                                       

                                      @Chris

                                       

                                      Thanks for the link to more resources on the topic. I'll check that out right now.

                                       

                                      p.s. Going to leave question as not been answered yet so Andrew's inquiry can get answered.

                                       

                                      Namaste,

                                      DK

                                      • 16. Re: Newbie Question (DispatchEvent is not reaching Main Application)
                                        DeyBwah Level 1

                                        Chris' resource link actually implicitly describes why bubbling is not true in all dispatchEvent situations.

                                         

                                        I wouldn't do justice to the explanation, so anyone who is interested should make the leap and check out the link. In a nutshell though, the reason why certain dispatchEvents bubble and certain ones don't is because it depends on where the dispatchEvent is taking place in the application.

                                         

                                        Bubbling is only one phase of the 3 phases in a dispatchEvent (other two are capturing and target).

                                         

                                        Depending on where the dispatch is taking place, certain phases are or are not present.

                                         

                                        So that clarifies Andrew's answer to my question further.

                                         

                                        Thanks guys!

                                        DK