4 Replies Latest reply on Jan 22, 2007 11:03 PM by chris.huston.t10

    Bindable variables in state which is not in use doesn't updated

    Camus Miu Level 1
      When the State changed to NEW_STATE, the Bindable value of the button doesn't match with the Singleton Varible.
      What is wrong here with the bindable stuff?

      I make the following code for testing. Can someone please gimme some idea, whats wrong with it?
      (The testing steps are in the mxml)
      Thanks a lot.

      ======================================================================================
      MXML
      ======================================================================================
      <?xml version="1.0" encoding="utf-8"?>
      <mx:Application xmlns:mx=" http://www.adobe.com/2006/mxml" layout="absolute"
      creationComplete="initApp();">
      <mx:states>
      <mx:State name="NEW_STATE">
      <mx:SetProperty target="{linkbutton1}" name="x" value="266"/>
      <mx:SetProperty target="{linkbutton1}" name="label"
      value="{SingletonClass.getInstance().variable2}"/>
      <mx:SetEventHandler target="{linkbutton1}" name="click" handler="currentState=''"/>
      </mx:State>
      </mx:states>
      <mx:Script>
      <![CDATA[
      import SingletonClass;

      private function initApp():void
      {
      SingletonClass.getInstance().changeLanguage(SingletonClass.LANGUAGE_BIG5);
      }

      ]]>
      </mx:Script>
      <mx:TextInput x="91" y="10" text="{SingletonClass.getInstance().variable1}"/>
      <mx:TextInput x="91" y="40" text="{SingletonClass.getInstance().variable2}"/>
      <mx:Label x="10" y="12" text="Variable_1:"/>
      <mx:Label x="10" y="42" text="Variable_2:"/>

      <mx:Button x="13" y="94" label="Chinese"
      click="SingletonClass.getInstance().changeLanguage(SingletonClass.LANGUAGE_BIG5)"/>
      <mx:Button x="104" y="94" label="English"
      click="SingletonClass.getInstance().changeLanguage(SingletonClass.LANGUAGE_ENG)"/>
      <mx:LinkButton x="176" y="144" label="{SingletonClass.getInstance().variable1}"
      id="linkbutton1" click="currentState='NEW_STATE'"/>
      <mx:Label x="104" y="146" text="Click Here: "/>
      <mx:TextArea x="10" y="181" width="475" height="208">
      <mx:text><![CDATA[
      1) Change the Language freely
      2) Press The LinkButton beside "Click here", The label and onClick action changed.
      3) Pree the LinkButton again, the position and label back to previous one.
      It works all fine.

      Problem comes here.
      1) Press the LinkButton and change to NEW_STATE.
      2) Change the language.
      3) Click the button again, and the language of the label doesn't make it.


      When the State changed to NEW_STATE, the Bindable value of the button doesn't match with the Singleton Varible.
      What is wrong here with the bindable stuff?
      ]]></mx:text>
      </mx:TextArea>
      </mx:Application>
      ======================================================================================
      SingletonClass.as
      ======================================================================================
      package
      {
      import flash.events.EventDispatcher;
      import flash.display.*;
      import flash.events.*;
      import flash.net.*;
      import flash.utils.*;
      import mx.styles.StyleManager

      [Bindable]
      public class SingletonClass
      {
      private var _variable1:String;
      private var _variable2:String;
      private static var _self:SingletonClass;

      // Big5
      private static var VARIABLE1_BIG5:String = "變數 1";
      private static var VARIABLE2_BIG5:String = "變數 2";

      // English
      private static var VARIABLE1_ENG:String = "Variable 1";
      private static var VARIABLE2_ENG:String = "Variable 2";

      public static var LANGUAGE_BIG5:String = "Big5";
      public static var LANGUAGE_ENG:String = "English";

      public function SingletonClass()
      {
      }

      public static function getInstance():SingletonClass
      {
      if(_self==null)
      {
      _self = new SingletonClass();
      }
      return _self;
      }

      public function changeLanguage(language:String):void
      {
      switch(language)
      {
      case LANGUAGE_BIG5:
      variable1 = VARIABLE1_BIG5;
      variable2 = VARIABLE2_BIG5;
      break;
      default:
      variable1 = VARIABLE1_ENG;
      variable2 = VARIABLE2_ENG;
      }
      }

      public function get variable1():String
      {
      return _variable1;
      }

      public function set variable1(str:String):void
      {
      _variable1 = str;
      }

      public function get variable2():String
      {
      return _variable2;
      }

      public function set variable2(str:String):void
      {
      _variable2 = str;
      }
      }
      }
        • 1. Re: Bindable variables in state which is not in use doesn't updated
          chris.huston.t10 Level 3
          I think the reason you are having variable problems is that the NEW_STATE is based on the base state and your event handler tells Flex to return to the base state, kind of like going back in a history list, so you see the original value of the LinkButton.

          You can avoid this problem by creating an additional state, thereby making the states independent of each other like this:

          <mx:states>
          <mx:State name="NEW_STATE">
          <mx:SetProperty target="{linkbutton1}" name="x" value="266"/>
          <mx:SetProperty target="{linkbutton1}" name="label" value="{SingletonClass.getInstance().variable2}"/>
          <mx:SetEventHandler target="{linkbutton1}" name="click" handler="currentState='NEW_STATE2'"/>
          </mx:State>
          <mx:State name="NEW_STATE2">
          <mx:SetProperty target="{linkbutton1}" name="x" value="176"/>
          <mx:SetProperty target="{linkbutton1}" name="label" value="{SingletonClass.getInstance().variable1}"/>
          <mx:SetEventHandler target="{linkbutton1}" name="click" handler="currentState='NEW_STATE'"/>
          </mx:State>
          </mx:states>

          Vygo
          • 2. Re: Bindable variables in state which is not in use doesn't updated
            Camus Miu Level 1
            It works now, let me examine on it and finialize the post later ^^

            thanks.
            ========================================================
            MXML
            ========================================================
            <mx:Application xmlns:mx=" http://www.adobe.com/2006/mxml" layout="absolute"
            creationComplete="initApp();">
            <mx:states>
            <mx:State name="NEW_STATE">
            <mx:AddChild position="lastChild">
            <mx:Button x="91" y="142" label="{SingletonClass.getInstance().variable1}" click="currentState='NEW_STATE2'"/>
            </mx:AddChild>

            </mx:State>
            <mx:State name="NEW_STATE2">
            <mx:AddChild position="lastChild">
            <mx:Button x="211" y="142" label="{SingletonClass.getInstance().variable2}" click="currentState='NEW_STATE'"/>
            </mx:AddChild>

            </mx:State>
            </mx:states>
            <mx:Script>
            <![CDATA[
            import SingletonClass;

            private function initApp():void
            {
            SingletonClass.getInstance().changeLanguage(SingletonClass.LANGUAGE_BIG5);
            currentState='NEW_STATE';
            }

            ]]>
            </mx:Script>
            <mx:TextInput x="91" y="10" text="{SingletonClass.getInstance().variable1}"/>
            <mx:TextInput x="91" y="40" text="{SingletonClass.getInstance().variable2}"/>
            <mx:Label x="10" y="12" text="Variable_1:"/>
            <mx:Label x="10" y="42" text="Variable_2:"/>

            <mx:Button x="13" y="94" label="Chinese"
            click="SingletonClass.getInstance().changeLanguage(SingletonClass.LANGUAGE_BIG5)"/>
            <mx:Button x="104" y="94" label="English"
            click="SingletonClass.getInstance().changeLanguage(SingletonClass.LANGUAGE_ENG)"/>


            <mx:TextArea x="10" y="181" width="475" height="208">
            <mx:text><![CDATA[
            1) Change the Language freely
            2) Press The LinkButton beside "Click here", The label and onClick action changed.
            3) Pree the LinkButton again, the position and label back to previous one.
            It works all fine.

            Problem comes here.
            1) Press the LinkButton and change to NEW_STATE.
            2) Change the language.
            3) Click the button again, and the language of the label doesn't make it.


            When the State changed to NEW_STATE, the Bindable value of the button doesn't match with the Singleton Varible.
            What is wrong here with the bindable stuff?
            ]]></mx:text>
            </mx:TextArea>
            <mx:Label x="10" y="144" text="Click Here:"/>
            </mx:Application>
            • 3. Re: Bindable variables in state which is not in use doesn't updated
              Camus Miu Level 1
              But isn't the [Bindable] will update all related values when it has "propertyChanged"??

              in this case <mx:TextInput x="91" y="10" text="{SingletonClass.getInstance().variable1}"/> of the unused original state is not updated. So bindable doesn't work in the scenerio, rite?

              Or I just need to prevent toggle the bindable variable in same display object during state change???
              • 4. Re: Bindable variables in state which is not in use doesn't updated
                chris.huston.t10 Level 3
                I am not sure I understand your problem. The TextInputs are both updating for me when I test it. Can you tell me the steps you use when the TextInput field does not reflect your singleton value? Everything seems to be working for me.

                Vygo