2 Replies Latest reply on Nov 5, 2010 11:50 PM by Shongrunden

    Custom Events: Parent issuing an event and child registering/handling

    larryl61 Level 1

      Can somebody please tell me what I'm doing wrong!!
      I'm am creating an custom event in a parent app and
      want the child to register for it and handle the event
      when sent!

       

      main app:
      <?xml version="1.0" encoding="utf-8"?>
      <s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
                  xmlns:s="library://ns.adobe.com/flex/spark"
                  xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600"
                  xmlns:local="*">
         <fx:Declarations>
            <!-- Place non-visual elements (e.g., services, value objects) here -->
         </fx:Declarations>
        
         <s:layout>
            <s:VerticalLayout />
         </s:layout>
        
         <fx:Metadata>
            [Event(name="ev", type="flash.events.Event")]
         </fx:Metadata>
        
         <fx:Script>
            <![CDATA[
               public var aa:int = 0;
              
               private function sendEvt():void
               {
                  var evt:Event = new Event("ev");
                  var ret:Boolean = dispatchEvent(evt);
               }
            ]]>
         </fx:Script>
        
        
         <s:Button label="Button 1" click="sendEvt()"/>
                
         <local:Component1 width="100%" height="100%">
         </local:Component1>
        
      </s:Application>

       

      Child APP *********************************************
      <?xml version="1.0" encoding="utf-8"?>
      <s:Group xmlns:fx="http://ns.adobe.com/mxml/2009"
             xmlns:s="library://ns.adobe.com/flex/spark"
             xmlns:mx="library://ns.adobe.com/flex/mx" width="400" height="300"
             creationComplete="init()">
         <fx:Declarations>
            <!-- Place non-visual elements (e.g., services, value objects) here -->
         </fx:Declarations>
        
         <fx:Script>
            <![CDATA[
               import mx.controls.Alert;
               private function init():void
               {
                  this.addEventListener("ev",evtHandler);
               }
              
               public function evtHandler(evt:Event):void
               {
                  Alert.show("HI");
               }
            ]]>
         </fx:Script>
        
      </s:Group>

        • 1. Re: Custom Events: Parent issuing an event and child registering/handling
          Claudiu Ursica Level 4

          Usually you do the other way around. Child talks to the parent through events.

          Parent calls methods directly on the child..

           

          So assuming that

           

          If  <local:Component1 id="theKid" width="100%" height="100%">

             </local:Component1>

          is the instance of your child group you can do this

           

          <s:Button label="Button 1" click="clickHandler(event)"/>

           

           

          private function clickHandler(event:MouseEvent):void

          {

              theKid.publicMethodToCall();

          }

           

          inside the child you declare the method as public

           

          public publicMethodToCall():WhateverType

          {

              trace("Called by the parent");

          }

           

          Unless I misunderstood somehow your use case.

          C

          • 2. Re: Custom Events: Parent issuing an event and child registering/handling
            Shongrunden Adobe Employee

             

            Your dispatching the event on the Application, but not listening on the Application.  Try dispatching the event on the same object you are listening on:

             

            var ret:Boolean = myComponent1.dispatchEvent(evt);

            ...

            <local:Component1 id="myComponent" width="100%" height="100%">

            </local:Component1>