1

The direct children of a ViewStack receive a FlexEvent.SHOW once the respective child view is shown. Unfortunately, the event information is lost at deeper child levels so that the grandchildren of the ViewStack do not receive the SHOW event.

Which approach would be advisable to tap into the SHOW events received by a ViewStack's direct children from a grandchild or grand-grand-child?

Using parent.parent.[...].addEventListener(FlexEvent.SHOW, [...]) would be possible, of course, but is kind of ugly as it will break as soon as the number of hierarchy levels between the two components changes.

Are there other events that are better suited?

The background of my question is that I would like to reload the grandchildren's content when it becomes visible again.

Thilo-Alexander Ginkel
  • 6,898
  • 10
  • 45
  • 58

2 Answers2

2

You could listen for the ADDED_TO_STAGE event in the grandchild view and find out if you're part of a view stack. If yes, then just listen to the view stack child's show/hide events.

... addedToStage="setMeUpForViewStack()" ...

Which is:

private var vsView:UIComponent = null;

private function setMeUpForViewStack():void
{
  if (vsView) {
    vsView.removeEventListener("show", vsViewShowHideHandler);
    vsView.removeEventListener("hide", vsViewShowHideHandler);
    vsView = null;
  }

  var obj:DisplayObject = this;
  while (obj.parent != obj) {
    if (obj.parent is ViewStack) {
      vsView = obj;
      break;
    }

    obj = obj.parent;
  }

  if (vsView) {
    vsView.addEventListener("show", vsViewShowHideHandler, false, 0, true);
    vsView.addEventListener("hide", vsViewShowHideHandler, false, 0, true);
  }
}

And in your vsViewShowHideHandler you would reload the content (if the view is visible).

Basically this frees your from worrying about the level of nesting. It doesn't work with multiple nested view stacks though.

In the REMOVED_FROM_STAGE event handler you would forget vsView.

Manish
  • 3,472
  • 1
  • 17
  • 16
0

Although burying down into the viewstack would work I agree this is ugly and potentially can lead to headaches as soon as something changes.

A different approach could be to implement a global event dispatcher.

By having a static event dispatcher in a class, the grandchildren could subscribe to events from the static dispatcher from anwywhere within the application.

When the parent hears the FlexEvent.Show the handler could dispatch a custom event using the global dispatcher?

James
  • 969
  • 1
  • 8
  • 13