• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1044
  • Last Modified:

Flex 3: Adding event listener on parent

I am writing a Flex application that contains a view stack with 2 custom components.  A child component on my first view stack item dispatches a custom event, which bubbles up to the application successfully.  I would like the event to be captured by my second view stack component.  I am calling parent.addEventListener in my creationComplete event of my second custom component, however, because this item has ot been navigated to/created yet when the event is dispatched, the event is not reaching this component.  Once I navigate to the second component, the init event is called and all subsequent events dispatched by the first component are captured successfully.

I have tried adding creationPolicy="all" on my second component, but the component is still not being created, and thus the init function for my creationComplete event is not being executed.

How can I get the second component to capture the custom event dispatched by my first component before it has been navigated to?
In application:

private function init():void {
       initialViewStack.selectedChild = dashboard;
       addEventListener(ViewChangeEvent.VIEWCHANGE, handleViewChangeEvent);
}

private function handleViewChangeEvent(event:ViewChangeEvent):void {  
      if (event.changeTo == ViewChangeEvent.DASHBOARD) {
            initialViewStack.selectedChild = dashboard;
      }
      else if (event.changeTo == ViewChangeEvent.MAINTENANCE) {
            initialViewStack.selectedChild = main;
      }                  
}

<mx:ViewStack id="initialViewStack" width="100%" height="100%" >
      <nd:Dashboard id="dashboard" />
      <nd:Main id="main" creationPolicy="all" />
</mx:ViewStack>

In DashBoard component:
private function linkBarClicked(eventLabel:String):void {
                  var e:ViewChangeEvent = new ViewChangeEvent(ViewChangeEvent.VIEWCHANGE,eventLabel, true);
      dispatchEvent(e);
}

In Main Component creationComplete="init()":
private function init():void {
     parent.addEventListener(ViewChangeEvent.VIEWCHANGE, handleViewChangeEvent);
}

Thanks.

0
trudyhlittle
Asked:
trudyhlittle
  • 3
  • 2
1 Solution
 
zzynxSoftware engineerCommented:
>> I have tried adding creationPolicy="all" on my second component

I think you should set that on your viewstack:

<mx:ViewStack id="initialViewStack" creationPolicy="all" width="100%" height="100%" >
0
 
trudyhlittleAuthor Commented:
Great, that now works, but it has created some additional problems.  There is actually an additional viewstack item that I removed from the example to simplify it.  Some of the information required by ths component hasen't been set yet when the application fires up.  This information is entered by the user prior to allowing them access the the component.   I really don't want all the viewstack items to be created when the main application starts up.  Is there a way to prevent some of the view stack items from not having the creationPolicy=all?  Thanks.
0
 
zzynxSoftware engineerCommented:
>> I really don't want all the viewstack items to be created when the main application starts up.
Then setting the viewStack's creationPolicy="all" is probably a bad idea (although it is a solution for your eventlistener's problem)

>> Is there a way to prevent some of the view stack items from not having the creationPolicy=all?
No. Since you don't set it on the stack items but on the stack itself. It is applied to all or none of them.

>> ... because this item has ot been navigated to/created yet when the event is dispatched, the event is not reaching this component.
I'm afraid you'll have to "store" this "knowledge" somewhere and apply it later on when the component is created.
0
 
trudyhlittleAuthor Commented:
OK.  Got it thanks.  I'm storing the data and applying it to the component.  It doesn't seem like a very OO solution, but it works.  Thanks for the help.
0
 
zzynxSoftware engineerCommented:
You're welcome.
Thanx 4 axxepting
0

Featured Post

[Webinar] Kill tickets & tabs using PowerShell

Are you tired of cycling through the same browser tabs everyday to close the same repetitive tickets? In this webinar JumpCloud will show how you can leverage RESTful APIs to build your own PowerShell modules to kill tickets & tabs using the PowerShell command Invoke-RestMethod.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now