help regarding ViewStates CurrentStateChange in flex

  • dhina.techno
  • Newbie
  • Newbie
  • dhina.techno
  • Posts: 8

Post 3+ Months Ago

Hi,
I am a newbie in flex 2.0 and action script 3.0.
When i press the button, i need two things to happen.
one is change my view state and other one is calling a inline actionscript function.
<mx:Button x="84" y="192" label="Button" id="Button1" click="getit()"/>
(where getit() is my inline function in actionscript).
i know how to create a new state and so on. But, i need to know, how can i make these two things happen in a single button click. I heard something called CurrentStateChange which handles this functionality. Kindly let me know how to use this CurrentStateChange, or if you have anything to say!

Your help is highly appreciated!

thanks,
Dhina
  • Anonymous
  • Bot
  • No Avatar
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post 3+ Months Ago

  • spork
  • Brewmaster
  • Silver Member
  • User avatar
  • Posts: 6251
  • Loc: Seattle, WA

Post 3+ Months Ago

Just to clear something up, when you say you're using an "inline" function, do you mean that you've declared it in a <mx:Script> tag, or did you write the function directly into the click event in the button tag? I'm going to assume the prior, correct me if I'm mistaken.

The easiest way to handle states is to use the currentState property of the object in question. Changing this property will change the state directly.

For example, you can set the starting state of your application in a variable:
Code: [ Select ]
<mx:Script>
  <![CDATA[
    
    [Bindable]
    var applicationState : String = "first_state";
    
  ]]>
</mx:Script>
  1. <mx:Script>
  2.   <![CDATA[
  3.     
  4.     [Bindable]
  5.     var applicationState : String = "first_state";
  6.     
  7.   ]]>
  8. </mx:Script>


Now, you want to bind this value to the application's state like so:
Code: [ Select ]
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
  layout="absolute"
  currentState="{applicationState}">
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
  3.   layout="absolute"
  4.   currentState="{applicationState}">

Notice how I've used data binding to bind the state of the application to the variable "applicationState". Now, to change the state of the application, you simply change this variable to the new state in your function:
Code: [ Select ]
private function getit() : void
{
  // your other code here...
  ...
  ...
  applicationState = "new_state";
}
  1. private function getit() : void
  2. {
  3.   // your other code here...
  4.   ...
  5.   ...
  6.   applicationState = "new_state";
  7. }

Good programming practice is to declare all the possible states as static constants that can easily be switched out.
  • dhina.techno
  • Newbie
  • Newbie
  • dhina.techno
  • Posts: 8

Post 3+ Months Ago

Hi Spork,
Thnx once again for replying..

the exact requirement is..
i have few textboxes... a button and a datagrid....

the "basestate" will have only textboxes and a button..

the gridstate will have previous textboxes,button and a datagrid...

i ll take user input from the textboxes, and while on clicking the button, the gridstate is called and also the <mx:script> function "getit()" is called to process the textbox data to the datagrid..

so, ultimately, my single button click should do two things..
1) change the state to the gridstate
2) process the data and bind it to the datagrid.

what im doing right now is..

<mx:Button x="84" y="192" label="Button" id="Button1" click="currentState = 'GridState'"/>

ie., im changing the state to GridState on button click.. but how to call the function "getit()"

hope u understood my problem..

thanks in advance,
Dhina.
  • spork
  • Brewmaster
  • Silver Member
  • User avatar
  • Posts: 6251
  • Loc: Seattle, WA

Post 3+ Months Ago

Well, you can call the function inline after changing the state, or add the state change to the function itself.
Code: [ Select ]
<mx:Button x="84" y="192" label="Button" id="Button1" click="currentState = 'GridState'; getit();"/>
  • dhina.techno
  • Newbie
  • Newbie
  • dhina.techno
  • Posts: 8

Post 3+ Months Ago

Hi Spork,
You rock man!
once again u've shown me the right path..

thanks very much for that..
I guess, i can take you as a lead n go on with my project :)

thanks,
Dhina
  • spork
  • Brewmaster
  • Silver Member
  • User avatar
  • Posts: 6251
  • Loc: Seattle, WA

Post 3+ Months Ago

Rocking is something that just comes naturally to me. Experience in Flex, well, that takes some practice.

Glad to help. Welcome to Ozzu :)
  • dhina.techno
  • Newbie
  • Newbie
  • dhina.techno
  • Posts: 8

Post 3+ Months Ago

hahaha.. thnx man!

Post Information

  • Total Posts in this topic: 7 posts
  • Users browsing this forum: No registered users and 9 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.