Search code examples
apache-flexeventsflex3adobecairngorm

Best way to do events with the PopUpManager?


If I am using the PopUpManager to add a child, and I dispatch an event from that child, the bubbles don't seem to bubble to the top of my application (main application).

For instance:

PopUpManager.addPopUp( popup, parentApplication as Application, false );

Then in the popup, I do:

dispatchEvent( new Event( "testEvent", true ) );

I have an eventListener in parentApplication (root .mxml) for "testEvent", but it never fires. Because of this, I have been dispatching events and listening for events on the ModelLocator (using cairngorm). This obviously is not ideal, because I have to manually make sure I remove the event listeners in a lot of cases.

Any ideas?

Thanks!


Solution

  • I'm more familiar with Mate than Cairngorm, but what I do in this situation is to back up my pop up with a model and dispatch the events off of the model. Since the model and the main application sit at the same level, the main application can hear the events.

    Update:

    Here's a rough example.

    In my MainMap.mxml I create an instance of the presentation model and inject it into my popup.

    <EventHandlers type="{ FlexEvent.PREINITIALIZE }">
      <ObjectBuilder generator="{ MyPopUpPresentationModel  }" constructorArguments="{ scope.dispatcher }"/>
    </EventHandlers>
    
    <Injectors target="{ MyPopUp }">
        <PropertyInjector targetKey="model" source="{ MyPopUpPresentationModel }"/>
    </Injectors>
    

    And In MyPopUp.mxml I have an instance of my model.

    <fx:Script>
      <![CDATA[
        [Bindable] public var model:MyPopUpPresentationModel;
      ]]>
    </fx:Script>
    

    Here's MyPopUpPresentationModel.as.

    package
    {
      private var dispatcher:IEventDispatcher;
      public function DigitalTagTrackingPresentationModel(target:IEventDispatcher)
      {
        this.dispatcher = target;
      }
    
      public function dispatchMyCustomEvent():void
      {
        dispatcher.dispatchEvent(new Event("MyCustomEvent"));
      }
    }
    

    When you call model.dispatchMyCustomEvent(); from MyPopUp.mxml it will dispatch the event using the scope of the presentation model's dispatcher which will be at the same level as the parent application. I hope this helps!