View Mediator and ViewNavigator

Page Contents:

View Mediator Support

Swiz includes two metadata tags which support the view mediator pattern. This allows references to views to be supplied to other beans. These two tags are [ViewAdded] and [ViewRemoved]. The tags have no attributes, so using them is very straightforward.

[ViewAdded] and [ViewRemoved] can be specified on properties, setters or methods in beans. Swiz will pass in any instances of the type specified that it detects have been added to or removed from stage. The events that trigger these actions are those specified in your SwizConfig's setUpEventType and tearDownEventType properties.

For example:

public function userListViewAdded( view : UserListView ) : void
    this.view = view;

public function userListViewRemoved( view : UserListView ) : void
    this.view = null;

The existence of these tags in a Prototype bean will not trigger the creation of that bean just because a matching view is added/removed. The bean instance containing these tags must already exist when the view event occurs.

ViewNavigator Metadata Tag

In Flex mobile projects, you can now use the [ViewNavigator] tag to get a reference to the ViewNavigator instance in non-view classes. This works in a manner similar to [ViewAdded]:

public var viewNavigator : ViewNavigator;

Note that a ViewNavigator injection is not guaranteed to be populated by the time [PostConstruct] methods run, due to the timing around the creation of the ViewNavigator by the Flex mobile SDK.