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.

trudyhlittleAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Adobe Flash

From novice to tech pro — start learning today.