8 Replies Latest reply on Nov 27, 2011 11:50 PM by drkstr_1

    Components in states

    leondogg

      when using states the component properties are assigned as

      propertie.state ="" e.g. <s:Button id="button1" enable.state1="true" />

      this works.

       

      but how do you change the value of the propertity within the fx:Script tags

       

      <fx:Script>

      component.propertity ="" does not work

      button1.enable="false"

       

      component.propertity.state ="" does not work

      button1.enable.state1 ="false"

       

      component.state.propertity ="" does not work

      button1.state1.enable="false"

       

      </fx:Script>

      how can i do this

        • 1. Re: Components in states
          drkstr_1 Level 4

          States are an MXML concept. It does not translate well to functional Actionscript.

           

          Usually state changes are driven by some kind of property change. Just call the appropriate AS code when those properties are changed. You can also listen for a stateChange event and call the appropriate code based on the currentState, but this doesn't seem like a natural solution to me.

          • 2. Re: Components in states
            leondogg Level 1

            so is it not possible to enable/disable a button or change the Button Label in AS when using states?

            • 3. Re: Components in states
              drkstr_1 Level 4

              It's absolutely possible. See my first post for two possible methodologies.

               

              If you are asking, "can I use declarative MXML syntax in AS", than the answer is no. Such a concept in a functional programming language would be bananas!

              1 person found this helpful
              • 4. Re: Components in states
                William Spence Level 1

                I had this same problem myself and came up with a fairly simple solution.  You create your states in MXML.  There is a an event called enterState that is dispatched when the state is entered that you can grab ahold of and make your changes in actionscript based on the states that you are in.  You must remember to make changes to all of the properties that you require in all 3 states.  So it would look something like this:

                 

                <fx:Script>
                                    <![CDATA[ 
                  
                                              private function state1Changes(event:FlexEvent):void
                                              {
                                                        button.enabled = true;
                                                        button.label = "State1";
                                              }
                  
                                              private function state2Changes(event:FlexEvent):void
                                              {
                                                        button.enabled = false; 
                                                        button.label = "State 2";
                                              }
                  
                                              private function state3Changes(event:FlexEvent):void
                                              {
                                                        button.enabled = true; 
                                                        button.label = "State 3";
                                              }
                  
                  
                                    ]]>
                          </fx:Script>
                
                
                          <s:states>
                                    <s:State name="state1" enterState="state1Changes(event)"/>
                                    <s:State name="state2" enterState="state2Changes(event)"/>
                                    <s:State name="state3" enterState="state3Changes(event)"/>
                          </s:states>
                
                

                 

                Be sure to note the enterState event and the handler where you create your states ub the <s:states> block.   Hope this helps!!!

                1 person found this helpful
                • 5. Re: Components in states
                  leondogg Level 1

                  thanks much, i understand this, but what i relly want is the property of the button to be changed after the state has all ready loaded.

                  being able to change button properties within a loaded state.

                  • 6. Re: Components in states
                    drkstr_1 Level 4

                    I don't fully understand your last post.

                     

                    Can you elaborate on what you are trying to accomplish, and the reason why the three different methodologies proposed would be insufficient?

                    • 7. Re: Components in states
                      leondogg Level 1

                      i amd an error through in experience and built an app with 16 states, some of them sharing buttons and other components. within the mxml i can use them in different states by dooing this:

                       

                      <s:Button id="button1" includeIn="state1, state2, state3" enable.state1="true" enable.state2="false" enable.state3="true" />

                       

                      however in a CRUD form (which is a loaded state) i want to disable a button after clicking it (while remaining in the same state) and re enable it after clicking another button.

                       

                      i i then found out i cant do this in a function button1.enable="true" cause it dose'nt work,

                       

                      the easy out for me is simply build a new button in mxml

                       

                      <s:Button id="button2" includeIn="state1 enable="true">

                      then

                      button1.enable="true" works in script

                       

                      i am just trying to find out y the other method does not work

                       

                      I do appricate your help

                      • 8. Re: Components in states
                        drkstr_1 Level 4

                        oh wow, i totally misunderstood the question.

                         

                        Ditch the quotes and it will work just fine in script.

                         

                        "true" is a String, while true is a Boolean.

                         

                        So in some function (like the click handler) you would have:

                         

                        button1.enabled = true;