Sunday, March 28, 2010

states

How do u get the current state of say the login form demo?

so i button clicked and current state is register do this else do this?

I've tried currentstate property but to no avail?

Cheers

Tim

states

selectedIndex

states

So in this case how would i use the selected index to trace the forms state when the button is pressed?

%26lt;?xml version=''1.0'' ?%26gt;
%26lt;mx:TitleWindow xmlns:mx=''http://www.adobe.com/2006/mxml''
?height=''324'' borderColor=''#C0C0C0'' width=''341''
?backgroundColor=''#FFFFFF'' cornerRadius=''20''
?creationComplete=''init()''%26gt;

%26lt;mx:Script%26gt;
?%26lt;![CDATA[
?public function init()
{
?loginButton.addEventListener(MouseEvent.CLICK, login_handler);?
}


?public function login_handler(e:MouseEvent)
{
?trace(loginForm.selectedIndex);
}?


?]]%26gt;
%26lt;/mx:Script%26gt;

?%26lt;!-- Define one view state, in addition to the base state.--%26gt;
?%26lt;mx:states%26gt;
?%26lt;mx:State name=''Register''%26gt;
?%26lt;mx:AddChild relativeTo=''{loginForm}'' position=''lastChild''%26gt;
?%26lt;mx:target%26gt;
?%26lt;mx:FormItem id=''email'' label=''Email:''%26gt;
?%26lt;mx:TextInput/%26gt;
?%26lt;/mx:FormItem%26gt;
?%26lt;/mx:target%26gt;
?%26lt;/mx:AddChild%26gt;
?%26lt;mx:SetProperty target=''{loginPanel}'' name=''title'' value=''Register''/%26gt;
?%26lt;mx:SetProperty target=''{loginButton}'' name=''label'' value=''Register''/%26gt;
?%26lt;mx:SetStyle target=''{loginButton}''
?name=''color'' value=''blue''/%26gt;
?%26lt;mx:RemoveChild target=''{registerLink}''/%26gt;
?%26lt;mx:AddChild relativeTo=''{spacer1}'' position=''before''%26gt;
?%26lt;mx:target%26gt;
?%26lt;mx:LinkButton id=''loginLink'' label=''Return to Login'' click=''currentState=''''/%26gt;
?%26lt;/mx:target%26gt;
?%26lt;/mx:AddChild%26gt;
?%26lt;mx:SetProperty target=''{loginPanel}'' name=''height'' value=''272''/%26gt;
?%26lt;/mx:State%26gt;
?%26lt;/mx:states%26gt;

?%26lt;mx:transitions%26gt;
?%26lt;!-- Define the transition from the base state to the Register state.--%26gt;
?%26lt;mx:Transition id=''toRegister'' fromState=''*'' toState=''Register''%26gt;
?%26lt;mx:Sequence targets=''{[loginPanel, registerLink, email, loginLink, spacer1]}''%26gt;
?%26lt;mx:RemoveChildAction/%26gt;
?%26lt;mx:SetPropertyAction target=''{loginPanel}'' name=''title''/%26gt;
?%26lt;mx:SetPropertyAction target=''{loginButton}'' name=''label''/%26gt;
?%26lt;mx:SetStyleAction target=''{loginButton}'' name=''color''/%26gt;
?%26lt;mx:Resize target=''{loginPanel}''/%26gt;
?%26lt;mx:AddChildAction/%26gt;
?%26lt;/mx:Sequence%26gt;
?%26lt;/mx:Transition%26gt;

?%26lt;!-- Define the transition from the Register state to the base state.--%26gt;
?%26lt;mx:Transition id=''toDefault'' fromState=''Register'' toState=''*''%26gt;
?%26lt;mx:Sequence targets=''{[loginPanel, registerLink,
?email, loginLink, spacer1]}''%26gt;
?%26lt;mx:RemoveChildAction/%26gt;
?%26lt;mx:SetPropertyAction target=''{loginPanel}'' name=''title''/%26gt;
?%26lt;mx:SetPropertyAction?target=''{loginButton}'' name=''label''/%26gt;
?%26lt;mx:SetStyleAction target=''{loginButton}'' name=''color''/%26gt;
?%26lt;mx:Resize target=''{loginPanel}''/%26gt;
?%26lt;mx:AddChildAction/%26gt;
?%26lt;/mx:Sequence%26gt;
?%26lt;/mx:Transition%26gt;
?%26lt;/mx:transitions%26gt;

?%26lt;!-- Define a Panel container that defines the login form.--%26gt;
?%26lt;mx:Panel title=''Login'' id=''loginPanel''
?horizontalScrollPolicy=''off'' verticalScrollPolicy=''off''
?paddingTop=''10'' paddingLeft=''10'' paddingRight=''10'' paddingBottom=''10''%26gt;
?
?%26lt;mx:Text width=''100%'' color=''#000000''
?text=''Enter your username and password to login, alternatively click register to create a new account.;''/%26gt;

?%26lt;mx:Form id=''loginForm'' %26gt;
?%26lt;mx:FormItem label=''Username:''%26gt;
?%26lt;mx:TextInput/%26gt;
?%26lt;/mx:FormItem%26gt;
?%26lt;mx:FormItem label=''Password:''%26gt;
?%26lt;mx:TextInput/%26gt;
?%26lt;/mx:FormItem%26gt;
?%26lt;/mx:Form%26gt;
?%26lt;mx:ControlBar%26gt;
?%26lt;mx:LinkButton id=''registerLink''?label=''Need to Register?''
?click=''currentState='Register'''/%26gt;
?%26lt;mx:Spacer width=''100%'' id=''spacer1''/%26gt;
?%26lt;mx:Button label=''Login'' id=''loginButton''/%26gt;
?%26lt;/mx:ControlBar%26gt;
?%26lt;/mx:Panel%26gt;
%26lt;/mx:TitleWindow%26gt;

Hi

To find the current state that is displayed you can use the currentState statement its read write.

trace(currentState);

Andrew

I've tried that but it echo's null??

This was my test

%26lt;?xml version=''1.0''?%26gt;

%26lt;mx:Application xmlns:mx=''http://www.adobe.com/2006/mxml'' creationComplete=''initCollections();'' %26gt;

?%26lt;mx:states%26gt;

?%26lt;mx:State name=''state2''%26gt;

?%26lt;mx:RemoveChild target=''{label1}''/%26gt;

?%26lt;mx:AddChild position=''lastChild''%26gt;

?%26lt;mx:Label text=''THIS IS STATE 2''/%26gt;

?%26lt;/mx:AddChild%26gt;

?%26lt;/mx:State%26gt;

?%26lt;/mx:states%26gt;

?%26lt;mx:Script%26gt;

?%26lt;![CDATA[

?import mx.events.MenuEvent;

?import mx.controls.Alert;

?import mx.collections.*;

?[Bindable]

?public var menuBarCollection:XMLListCollection;

?private var menubarXML:XMLList =

?%26lt;%26gt;

?%26lt;menuitem label=''Menu1'' data=''top''%26gt;

?%26lt;menuitem label=''First State'' data=''''/%26gt;

?%26lt;menuitem label=''Second State'' data=''state2''/%26gt;

?%26lt;/menuitem%26gt;

?%26lt;/%26gt;;

? private function initCollections():void {

?menuBarCollection = new XMLListCollection(menubarXML);

?}

?private function menuHandler(event:MenuEvent):void?{

?currentState = event.item.@data;

?}

]]%26gt;

?%26lt;/mx:Script%26gt;

%26lt;mx:MenuBar labelField=''@label'' itemClick=''menuHandler(event);''

?dataProvider=''{menuBarCollection}'' /%26gt;

%26lt;mx:Label text=''THIS IS STATE 1 - DEFAULT'' id=''label1''/%26gt;

%26lt;mx:Button label=''Show state'' click=''{mx.controls.Alert.show(currentState)}''/%26gt;

%26lt;/mx:Application%26gt;

Andrew

any idea how I can adapt my example to trace the current state?

This works for me

%26lt;?xml version=''1.0'' ?%26gt;

%26lt;mx:TitleWindow xmlns:mx=''http://www.adobe.com/2006/mxml''

?height=''324'' borderColor=''#C0C0C0'' width=''341''

?backgroundColor=''#FFFFFF'' cornerRadius=''20''

?creationComplete=''init()''%26gt;

%26lt;mx:Script%26gt;

?%26lt;![CDATA[

? import mx.controls.Alert;

?public function init():void

{

?loginButton.addEventListener(MouseEvent.CLICK, login_handler);?

}

?public function login_handler(e:MouseEvent):void

{

?mx.controls.Alert.show(currentState);

}

?]]%26gt;

%26lt;/mx:Script%26gt;

?%26lt;!-- Define one view state, in addition to the base state.--%26gt;

?%26lt;mx:states%26gt;

?%26lt;mx:State name=''Register''%26gt;

?%26lt;mx:AddChild relativeTo=''{loginForm}'' position=''lastChild''%26gt;

?%26lt;mx:target%26gt;

?%26lt;mx:FormItem id=''email'' label=''Email:''%26gt;

?%26lt;mx:TextInput/%26gt;

?%26lt;/mx:FormItem%26gt;

?%26lt;/mx:target%26gt;

?%26lt;/mx:AddChild%26gt;

?%26lt;mx:SetProperty target=''{loginPanel}'' name=''title'' value=''Register''/%26gt;

?%26lt;mx:SetProperty target=''{loginButton}'' name=''label'' value=''Register''/%26gt;

?%26lt;mx:SetStyle target=''{loginButton}''

?name=''color'' value=''blue''/%26gt;

?%26lt;mx:RemoveChild target=''{registerLink}''/%26gt;

?%26lt;mx:AddChild relativeTo=''{spacer1}'' position=''before''%26gt;

?%26lt;mx:target%26gt;

?%26lt;mx:LinkButton id=''loginLink'' label=''Return to Login'' click=''currentState=''''/%26gt;

?%26lt;/mx:target%26gt;

?%26lt;/mx:AddChild%26gt;

?%26lt;mx:SetProperty target=''{loginPanel}'' name=''height'' value=''272''/%26gt;

?%26lt;/mx:State%26gt;

?%26lt;/mx:states%26gt;

?%26lt;mx:transitions%26gt;

?%26lt;!-- Define the transition from the base state to the Register state.--%26gt;

?%26lt;mx:Transition id=''toRegister'' fromState=''*'' toState=''Register''%26gt;

?%26lt;mx:Sequence targets=''{[loginPanel, registerLink, email, loginLink, spacer1]}''%26gt;

?%26lt;mx:RemoveChildAction/%26gt;

?%26lt;mx:SetPropertyAction target=''{loginPanel}'' name=''title''/%26gt;

?%26lt;mx:SetPropertyAction target=''{loginButton}'' name=''label''/%26gt;

?%26lt;mx:SetStyleAction target=''{loginButton}'' name=''color''/%26gt;

?%26lt;mx:Resize target=''{loginPanel}''/%26gt;

?%26lt;mx:AddChildAction/%26gt;

?%26lt;/mx:Sequence%26gt;

?%26lt;/mx:Transition%26gt;

?%26lt;!-- Define the transition from the Register state to the base state.--%26gt;

?%26lt;mx:Transition id=''toDefault'' fromState=''Register'' toState=''*''%26gt;

?%26lt;mx:Sequence targets=''{[loginPanel, registerLink,

?email, loginLink, spacer1]}''%26gt;

?%26lt;mx:RemoveChildAction/%26gt;

?%26lt;mx:SetPropertyAction target=''{loginPanel}'' name=''title''/%26gt;

?%26lt;mx:SetPropertyAction?target=''{loginButton}'' name=''label''/%26gt;

?%26lt;mx:SetStyleAction target=''{loginButton}'' name=''color''/%26gt;

?%26lt;mx:Resize target=''{loginPanel}''/%26gt;

?%26lt;mx:AddChildAction/%26gt;

?%26lt;/mx:Sequence%26gt;

?%26lt;/mx:Transition%26gt;

?%26lt;/mx:transitions%26gt;

?%26lt;!-- Define a Panel container that defines the login form.--%26gt;

?%26lt;mx:Panel title=''Login'' id=''loginPanel''

?horizontalScrollPolicy=''off'' verticalScrollPolicy=''off''

?paddingTop=''10'' paddingLeft=''10'' paddingRight=''10'' paddingBottom=''10''%26gt;

?%26lt;mx:Text width=''100%'' color=''#000000''

?text=''Enter your username and password to login, alternatively click register to create a new account.;''/%26gt;

?%26lt;mx:Form id=''loginForm'' %26gt;

?%26lt;mx:FormItem label=''Username:''%26gt;

?%26lt;mx:TextInput/%26gt;

?%26lt;/mx:FormItem%26gt;

?%26lt;mx:FormItem label=''Password:''%26gt;

?%26lt;mx:TextInput/%26gt;

?%26lt;/mx:FormItem%26gt;

?%26lt;/mx:Form%26gt;

?%26lt;mx:ControlBar%26gt;

?%26lt;mx:LinkButton id=''registerLink''?label=''Need to Register?''

?click=''currentState='Register'''/%26gt;

?%26lt;mx:Spacer width=''100%'' id=''spacer1''/%26gt;

?%26lt;mx:Button label=''Login'' id=''loginButton''/%26gt;

?%26lt;/mx:ControlBar%26gt;

?%26lt;/mx:Panel%26gt;

%26lt;/mx:TitleWindow%26gt;

If you click login in the baseState then its gonna show blank, cos the default state is ''''.?When in register it works.

I've removed the loginComp.currentState and just got currentState cos that refers to 'this', the comp its in

Does that help

Andrew

perfect, thanks for your help!

Just one more thing tho, when I open the register part then trace it shows register, should it not then trace login when I switch back to the base state?

No because you do not have a state called login.?The base state is referred to as ''''.

ie to set the state to register you do

currentState = 'Register'

and to set it back to the base state

currentState = ''

Thats the way it works

Andrew

No comments:

Post a Comment