This example shows you how to display or hide content in your Royale application, thanks to the View States feature. It uses the new Jewel UI set that supports themes and is available in the 0.9.4 release or later.
<?xml version=“1.0” encoding=“UTF-8”?>
<j:Card id=“loginForm” includeIn=“login”>
<j:Card id=“loggedInForm” includeIn=“loggedIn”>
The View States feature is a way of putting different filters over part of the application so that different things appear depending on what the app is doing, what permissions the user has, what the user has just done, or some other condition. You create a series of “states” and associate components of your application with one or more of the states. The way you associate a component with a state is to add the includeIn attribute to the component and set the attribute equal to one or more of the available states. Once you have associated a parent component to a state, all of its children (the things “inside” it) are also associated to the state.
Using states gives you a lightweight and quick way of updating what the user sees, without having to provide all sorts of modules that have to be loaded and unloaded.
The example shows using states to switch between two Card components, what you see before logging in and what you see once you have logged in. To make this work you need to add the SimpleStatesImpl bead. Then define the states you need in the State class. Here, our two states are login and loggedIn. Finally, add an “includeIn” attribute to each Card component and associate it with a state.
When you first see the app, the currentState in the view is the first state listed in the State class: login.
When you click the button, the “click” function assigns a new state as the currentState. Anything that is not associated with the new state magically disappears, and anything that is associated with it is suddenly visible.
Instead of using includeIn, you have another way via dot notation in attributes, in this case notice the notation visible=”true” and visible.<state>=”false” in the following code:
<j:Card id=“loginForm” visible=“true” visible.loggedIn=“false”>
<html:H1 text=“Royale login”/>
<j:TextInput id=“username” text=“someuser”/>
<j:TextInput id=“password” text=“somepass”>
<j:Button text=“Login” emphasis=“primary” click=“view.currentState=”loggedIn””/>
<j:Card id=“loggedInForm” visible=“false” visible.loggedIn=“true”>
You can use it in almost any attribute you want, not only visible.
Another way to set SimpleStatesImpl is vía CSS. You can do this in MXML or in an external CSS file:
Where to go from here
The result of this code snippet is the following: