4 Replies Latest reply on Mar 4, 2009 7:26 PM by Newsgroup_User

    How to update a class property

    SiHoop
      I want to create a class that has a property that updates when I press a button. The property does not update-- what am I doing wrong? Thank you.
        • 1. Re: How to update a class property
          Level 7
          GoalsPanel.mxml
          <?xml version="1.0" encoding="utf-8"?>
          <mx:Panel xmlns:mx=" http://www.adobe.com/2006/mxml">
          <mx:Script>
          <![CDATA[
          [Bindable]
          public var someVal:String;
          ]]>
          </mx:Script>
          <mx:TextArea id="myText" text="{someVal}"/>
          </mx:Panel>

          The other option would be to use a getter and setter function, and make
          the change in the setter method. This is generally preferable,
          especially if you need to do processing, because you can "dirty" the
          value in the setter, and then do any processing you need to in
          commitProperties().

          Also, you should give the GoalsPanel a name that's different than its
          class name. Usually, instances of classes use lower camel case, so in
          this case a good name would be "goalsPanel".
          • 2. Re: How to update a class property
            SiHoop Level 1
            Thanks. I've just discovered what getter setter methods can do for me-- that was worth the struggle!
            • 3. Re: How to update a class property
              SiHoop Level 1
              I've just run into a problem with the getter setter functions. I have them in a component and the functions are executing too soon. I'd like to put them in an init function and use creationComplete in the entry tag to trigger the init function, but I can't put the getter setter functions into another function. How should I work around the problem?
              • 4. Re: How to update a class property
                Level 7
                I don't think what you really want to do is use the creation complete
                event. I'm assuming that by "executing too soon", you mean that the code
                is setting a value in a child before the child has been created.

                There are a couple of solutions to this. First, instead of using getters
                and setters, use straight binding on the value:

                <?xml version="1.0" encoding="utf-8"?>
                <mx:Panel xmlns:mx=" http://www.adobe.com/2006/mxml">
                <mx:Script>
                <![CDATA[
                [Bindable]
                public var someVal:String;
                ]]>
                </mx:Script>
                <mx:TextArea id="myText" text="{someVal}"/>
                </mx:Panel>

                The second is to use getters and setters with binding, which means that
                you'll have to dispatch an event in your setter method.

                The third, which I hinted at in my previous post, is to use the
                commitProperties method to do your dirty work. For me, this is the
                preferred method, because it means that the processing will happen at
                most once per frame.

                <mx:Panel xmlns:custom="components.*"
                xmlns:mx=" http://www.adobe.com/2006/mxml" >
                <mx:Script>
                <![CDATA[
                .
                .
                var _someVal:int = 0;
                private var someValChanged:Boolean;
                public function set someVal (state:int):void {
                _someVal = state;
                someValChanged = true;
                invalidateProperties();
                }
                public function get someVal():int {
                return _someVal;
                }
                override public function commitProperties():void {
                super.commitProperties();
                if (someValChanged) {
                someValChanged = false;
                drawStudentGoalLine(someVal);
                }
                }

                .
                .
                .
                </mx:Panel>

                If you want to learn more about what this is doing, look for information
                about the component lifecycle. It's pretty integral to how Flex
                components work.