1 Reply Latest reply on Apr 19, 2008 1:45 PM by Brent Wientjes

    View-View Event Processing

    Brent Wientjes
      I am not getting View to View Event processing to work. I have looked through most of the Flex 2 and Flex 3 docs plus several of the Flex books to try to get this to work. I am only trying to get one view to create an event with additional information, pass it to another view which intercepts and then uses the event information. My simple exploratory code is:

      VIEW1 - Initiating view:

      <?xml version="1.0" encoding="utf-8"?>
      <mx:Canvas xmlns:mx=" http://www.adobe.com/2006/mxml" creationComplete="init()">
      <mx:Metadata>
      [Event(name = "monitorChange", type = "events.MonitorEvent")]
      </mx:Metadata>
      <mx:Script>
      <![CDATA[
      import mx.events.*;
      import events.MonitorEvent;
      [Bindable]
      private var test:String = "initial";
      private function init():void
      {this.addEventListener(MonitorEvent.MONITOR_CHANGE, monitorChange);
      test = "add event listener";
      }
      private function monitorChange(event:MonitorEvent):void
      {test = "add monitor event: " + String(event.ct);
      }
      private function startEvent():void
      {var e:MonitorEvent = new MonitorEvent("monitorChange", 5);
      this.dispatchEvent(e);
      }
      private function clearLabel():void
      {test = "clear listener";
      }
      ]]>
      </mx:Script>
      <mx:Button label="Event" left="300" top="8" click="startEvent()"/>
      <mx:Button label="Clear" left="380" top="8" click="clearLabel()"/>
      <mx:Label text="{test}" left="700" top="8"/>
      </mx:Canvas>

      VIEW2 – receiving view:

      <?xml version="1.0" encoding="utf-8"?>
      <mx:Canvas xmlns:mx=" http://www.adobe.com/2006/mxml" creationComplete="init()">
      <mx:Script>
      <![CDATA[
      import mx.events.*;
      import events.MonitorEvent;

      [Bindable]
      private var test:String = "initial";

      private function init():void
      {this.eventTest.addEventListener(MonitorEvent.MONITOR_CHANGE, monitorChange);
      test = "add event listener";
      }
      private function monitorChange(event:MonitorEvent):void
      {test = "add monitor event";
      }
      ]]>
      </mx:Script>
      <mx:Label id="eventTest" text="{test}" left="700" top="8"/>
      </mx:Canvas>

      CUSTOM EVENT:

      package events
      {import flash.events.Event;
      public class MonitorEvent extends Event
      {
      public function MonitorEvent(type:String, ct:int=0)
      {super(type);
      this.ct = ct;
      }
      public static const MONITOR_CHANGE:String = "monitorChange";
      public var ct:int;

      override public function clone():Event
      {return new MonitorEvent(type, ct);
      }
      }
      }

      The event is started in VIEW1 and processed but is not picked up in VIEW2. I can see that the listener in VIEW2 is loaded but nothing happens in VIEW2 when started in VIEW1. I am sure I am doing something stupid or not seeing the obvious. I would greatly appreciate help in getting this to work since this seems like the most straight forward way to pass information between asynchronous events within an application and keep everything sync’ed.
        • 1. Re: View-View Event Processing
          Brent Wientjes Level 1
          I think I have figured out the missing piece. As the event bubbles up from the original view, it only goes up the application tree. It does not take any of the branches off the tree. The 2 views were on parallel branches and thus the 2nd view never responded to the event. The solution is have a listener respond to the event above the 1st view that will initiate action in the 2nd view. In my case the parent of VIEW1 and VIEW2 are the same. Have parent listen to VIEW1 event and respond by calling appropriate routine in VIEW2.

          Simple concept but I missed it in all the reading.