Second revision of my Flex homework

Yesterday I posted the first version of the Flex assignment I created for myself. (For more information about what I’m doing, read this entry.)

I mentioned that I had a bit of trouble with states but that I felt I was slowly getting it. However - after speaking a bit with Mike Kollen, I think I realized that I was using states incorrectly. He said that he only uses states when he is modifying controls on screen. An example of this would be a search form that has an “Advanced” mode with more buttons. He said for full “change the whole page” type operations, ViewStacks may be more appropriate.

So I switched my code from using States to ViewStacks. The nice thing about ViewStacks is that they work exactly like I imagined States would. You basically wrap a set of components in the ViewStack, and only one child is visible at a time. Here is the new ViewStack-ified version of my code:

<mx:ViewStack id="mainView" width="100%" height="100%">

&lt;mx:HBox horizontalAlign="center" verticalAlign="middle" id="loginStage" width="100%" height="100%"&gt;
	&lt;mx:Panel id="loginScreen" title="The Logon Screen" width="500"&gt;
		&lt;mx:Form id="login" defaultButton="{loginButton}"&gt;
			&lt;mx:FormItem label="Username:" required="true"&gt;
					&lt;mx:TextInput id="username" /&gt;
			&lt;mx:FormItem label="Password:" required="true"&gt;
					&lt;mx:TextInput id="password" displayAsPassword="true" /&gt;
		&lt;mx:ControlBar horizontalAlign="right"&gt;
			&lt;mx:Button id="loginButton" label="Login" click="checkForm()" /&gt;

&lt;mx:HBox id="mainStage" width="100%" height="100%"&gt;
	&lt;mx:Text text="Main Stage" /&gt;	

</mx:ViewStack> </code>

Notice that my two HBoxs act like pages of the application. How do I switch my current page? My checkForm function now does this:


mainView was the name of the ViewStack container itself. If you want to see the complete code, go here:

You can also right click/view source on this demo to see the complete code.

Next up is the actual authentication. I’m going to do the simple Flash Remoting call at first and then look at how I can use this to call secured CFC methods.

Open Question: Look at the code I used to select the ViewStack. That feels fragile to me. What if, for some reason, I swap the children of my ViewStack? It seems like it would be nicer to do something like:

mainView.selectedChild("id of the child");

As far as I know, ViewStacks don’t support that - but a utility function could be created to do that I suppose. Thoughts?

Raymond Camden's Picture

About Raymond Camden

Raymond is a developer advocate. He focuses on JavaScript, serverless and enterprise cat demos. If you like this article, please consider visiting my Amazon Wishlist or donating via PayPal to show your support.

Lafayette, LA