2 Replies Latest reply on Nov 24, 2008 12:57 PM by peteandrus

    Custom Event: am i thinking about this correctly?

    peteandrus
      i need to know if i am understand this correctly.

      I want to do the following:
      1-make a custom event that will hold some information that i need dispatched when the event happens.
      2-dispatch the event from custom component 1.
      3-listen for and catch the event in a totally different custom component 2.

      It seems so trivial, so basic, but i can't get it to work correctly. is there some common mistake made in this?

      Here is the custom event code:
      package SURS.events
      {
      import SURS.JSURSReport;
      import flash.events.Event;
      public class JSURSReportClickEvent extends Event
      {
      static public const JSURSReportClick:String = "JSURSReportClick";
      private var reportClicked:JSURSReport;

      public function JSURSReportClickEvent(inReport:JSURSReport)
      {
      super("JSURSReportClick",true);
      reportClicked = inReport;
      }

      public function getJSURSReport():JSURSReport
      {
      return reportClicked;
      }

      override public function clone():Event {
      return new JSURSReportClickEvent(reportClicked);
      }
      }
      }

      so, in component B i add the actionListener:
      this.addEventListener("JSURSReportClick",eventHandlerFunction);
      (i put a breakpoint on this line and verified that it is, indeed, being executed)

      in component A i declare that this event is dispatched using metadata:
      <mx:Metadata>[Event(name="JSURSReportClick", type="SURS.events.JSURSReportClickEvent")]</mx:Metadata>

      eventually, i dispatch the event:
      var click:JSURSReportClickEvent = new JSURSReportClickEvent(clicked as JSURSReport);
      dispatchEvent(click);

      i have a breakpoint in the 'eventHandlerFunction' of component B, but it never hits after i dispatch the event!
      Also, i added the same listener to the component A (but with a different event handler) and it caught the event. This does me no good, though, because i need it to catch in component B! i just add that so you know that this custom event is somewhat legit. I just CANNOT get component B to catch it!

      any help is GREATLY appreciated!

        • 1. Re: Custom Event: am i thinking about this correctly?
          SujitG Level 2
          Hi,

          There are two ways you can listen to events.

          1. Register for events on the object instance which is dispatching the event. That is if object2 is dispatching an event, then you should have object2.addEventListener().. Events depend on the object instances.

          2. Events can be bubbled. This means you can register for an event dispatched by object2 on its parent. lets say object1 is parent of object2 and the event which is dispatched has bubbles property to true then object1.addEventListener is enough for handling the dispatched event.

          Please find more details at the URL below.

          http://livedocs.adobe.com/flex/3/html/events_02.html

          Hope this helps.
          • 2. Re: Custom Event: am i thinking about this correctly?
            peteandrus Level 1
            So, do i understand this correctly:

            to dispatch an event FROM component1 and hear it in component2, then one of the following must be true:

            1-component 1 must 'know about' component2 (a reference to component1 must be passed to 2)

            2-component2 must be a parent of component 1, and 'bubbles' must be true in my custom event, so that the event 'bubbles' up to its parent.

            I guess that makes sense now that i really think about it. I guess reporting each component to see if it has listeners for every event would kill the system quickly, so instead of a generalized reporting system like that this paradigm is used.

            So then, what is the best way to structure event dispatches and listeners for a large-scale application that will have lots of custom components all over the place? I don't want to be passing references to tons of components all over the place..... any suggestions/links for addressing this? thanks for the help!