4 Replies Latest reply on Apr 3, 2009 4:51 AM by salut

    addEventListener not listening

    salut
      I've Central.mxml, which is the application.
      In my project, under the sub-folder called view are FileA.mxml and FileB.mxm.

      I stripped the code to the minimum. I can't figure out why the addEventListener in Central.mxml is not listening to the dispatchEvent in FileA.mxml, which has the metatag like this:

      <mx:Metadata>
      [Event(name='iHaveChanged', type='flash.events.Event')]
      </mx:Metadata>

      I'd appreciate any pointer you may have.

      Attached Code:

      Central.mxml

      <?xml version="1.0"?>

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

      xmlns:view="view.*"

      width="100%"
      height="100%"

      creationComplete="init()"
      >




      <mx:Script>
      <![CDATA[
      import mx.controls.Alert;
      import view.FileA;
      import mx.events.*;

      [Bindable] public var test:String;
      public var fileA:FileA = new FileA();




      public function init():void
      {
      onCreationComplete();
      }



      private function onCreationComplete():void
      {
      Application.application.addEventListener('iHaveChanged', aHasChangedHandler);
      //fileA.addEventListener('iHaveChanged', aHasChangedHandler);
      }



      private function aHasChangedHandler(e:Event):void
      { Alert("aHasChangedHandler");
      var a:FileA = e.currentTarget as FileA;
      var aProp:String = a.text_input_FileA.text//"blah";
      Alert.show("aProp = " + aProp);

      var fileb:FileB = new FileB;
      fileb.varB = aProp;
      fileb.text_input_FileB.text = aProp;
      }

      ]]>
      </mx:Script>



      <mx:Box id="applicationBranding"
      width="100%"
      direction="horizontal">
      <mx:Text text="ZZZ Publisher"
      fontSize="24"
      />
      </mx:Box>
      <mx:Label text="Publication Creation and Delivery" color="yellow" />
      <mx:ApplicationControlBar id="applicationControlBar"
      width="100%"
      >
      <mx:LinkBar id="moduleLinkBar"
      direction="horizontal"
      horizontalAlign="left"
      dataProvider = "publisherViewStack"
      />
      <mx:Spacer width="100%" />
      <mx:LinkButton id="logoutbutton" label="Logout" />

      </mx:ApplicationControlBar>



      <mx:ViewStack id="publisherViewStack"
      width="100%"
      height="100%"
      >

      <view:Home id="home"
      label="Home"
      />

      <view:FileA id="filea"
      label="filea"
      />

      <view:FileB id="fileb"
      label="fileb"
      />

      </mx:ViewStack>


      </mx:Application>
      ---------------------------------------------------------
      FileA.mxml

      <?xml version="1.0" encoding="utf-8"?>
      <mx:VBox xmlns:mx=" http://www.adobe.com/2006/mxml"
      xmlns:view="view.*"
      width="100%"
      height="100%"
      creationComplete="init();"
      >

      <mx:Metadata>
      [Event(name='iHaveChanged', type='flash.events.Event')]
      </mx:Metadata>

      <mx:Script>
      <![CDATA[
      import mx.controls.Alert;
      import mx.events.*;
      [Bindable] public var varA:String;

      public function init():void
      {

      }



      private function onSomethingHappened(e:Event):void {
      //tell parent application that it needs to change the variable on b
      Alert.show("onSomethingHappened");
      var newEvent:Event = new Event('iHaveChanged');
      dispatchEvent(newEvent);
      Alert.show("dispatchEvent");
      }


      ]]>
      </mx:Script>

      <mx:TextInput id="text_input_FileA" text="lele" change="onSomethingHappened(event)"/>
      </mx:VBox>

      -------------------------------------------------------------------------
      FileB.mxml

      <?xml version="1.0" encoding="utf-8"?>
      <mx:VBox xmlns:mx=" http://www.adobe.com/2006/mxml"
      xmlns:view="view.*"
      width="100%"
      height="100%"
      creationComplete="init();"
      >


      <mx:Script>
      <![CDATA[
      import mx.controls.Alert;
      import mx.events.*;

      [Bindable] public var varB:String;

      public function init():void
      {
      //Alert.show("varB = " + varB);
      }

      public function getVarB():String
      {
      Alert.show("getVarB = " + varB);
      return varB;
      }




      ]]>
      </mx:Script>

      <mx:TextInput id="text_input_FileB" text="{getVarB()}"/>
      </mx:VBox>