Skip to main content

Note: how to change the current view from a command

In an application with more than one view it should be possible for a command to change the current view. For example, a LoginUser command should be able to hide the login form view and show another view.

This is achieved by having a currentView public property in the AppModelLocator class. The view container can then bind to this property. If a command changes the property, then the selected view changes. For example, in AppModelLocator:

public static const INITIAL_VIEW : Number = 0;
public static const ALTERNATIVE_VIEW : Number = 1;
public var currentView : Number = INITIAL_VIEW;
And the ViewStack container in Main.MXML contains the possible views (in the order corresponding to the constants) and is bound to model.currentView:

<mx:ViewStack width="100%" paddingBottom="10" paddingTop="10" resizeToContent="true" selectedIndex="{model.currentView}">
<view:InitialView />
<view:AlternativeView />
</mx:ViewStack>
So changing model.currentView to AppModelLocator..ALTERNATIVE_VIEW switches to the second view.