7 Replies Latest reply on Mar 22, 2011 2:06 AM by ckdkdkdlkdjfkljlkmvjklj

    Databinding

    ckdkdkdlkdjfkljlkmvjklj

      Hi !

       

      I read the documentation about databinding but I still encounter some problems in my application..

      The actionscript class looks like this (Only kept the important parts) :

       

           [Bindable]
           public class Alarm
           {
                
                     public var _tag:String;
                     public var _limits:ArrayCollection;
      
      
                      public function Alarm() {}
      
                public function loadAlarm(name:String) : void {
                          this.tag = name;
                          this.limits = new ArrayCollection([
                                    new Limit ("HH", 10),
                                    new Limit ("HI", 9),
                                    new Limit ("LL", 3),
                                    new Limit ("LO", 3.5)
                     ]);
      
                }
      
      

       

       

      Now when I load my application there is no information displayed because of the empty constructor off course.

      When I then call the loadAlarm function with a button at runtime. The limits are displayed in a DataGrid

      but the tag stays empty...

      Both _tag and _limits have getters and setters and they are also public.

      I have putted a trace in the setter and getter of _tag and the function is called and the correct name is put into the tag

      so the value definitely changes !

       

      Here is are the mxml snippets :

      ....

       

           <fx:Script>
                <![CDATA[
                     import model.Alarm;
                     import mx.binding.utils.BindingUtils;
                     import mx.controls.Text;
                     import mx.events.ListEvent;
                     
                     [Bindable]
                     private var myAlarm:Alarm = new Alarm();
      
      
                     public function onClick(evt:Event):void 
                     {  
                          myAlarm.loadAlarm("newName");
                          trace(myAlarm._tag);
                     } 
      
      
                ]]>
           </fx:Script>
      

       

      ...

       

      <mx:DataGrid width="147" height="306" id="alarmColumns" dataProvider="{myAlarm.limits}">
           <mx:columns>
           <mx:DataGridColumn id="alarms" headerText="Alarm" dataField="name"/>
           <mx:DataGridColumn id="limits" headerText="Limit" dataField="value"/>
           </mx:columns>
      </mx:DataGrid>
      

       

      ...

       

      <s:List width="167" height="306" id="alarmlist" selectedIndex="0" enabled="true">
           <mx:ArrayCollection>
                <fx:String>{myAlarm.tag}</fx:String>
           </mx:ArrayCollection>               
      </s:List>
      

       

      I really don't get why limits is changed but tag is not... Also the trace outputs the "newName" so it is definitely set !

       

      Many Thanks !

        • 1. Re: Databinding
          ckdkdkdlkdjfkljlkmvjklj Level 1
          myAlarm.loadAlarm("newName") => quotes missing because of syntax highlighting
          • 2. Re: Databinding
            huangxinghui Level 1

            why not use myAlarm._limits?

             

            <mx:DataGrid width="147" height="306" id="alarmColumns" dataProvider="{myAlarm._limits}">

             

            or change Alarm definition

             

            import mx.collections.ArrayCollection;

            [Bindable]
            public class Alarm
            {
              public var _tag:String;
                    public var _limits:ArrayCollection;

              public function Alarm()
              {
              }
              public function loadAlarm(name:String) : void
              {
                        this._tag = name;
                        limits = new ArrayCollection([
                                  new Limit ("HH", 10),
                                  new Limit ("HI", 9),
                                  new Limit ("LL", 3),
                                  new Limit ("LO", 3.5)
                       ]);
                }
               
                public function get limits():ArrayCollection
                {
                 return this._limits;
                }
               
                public function set limits(value:ArrayCollection):void
                {
                 this._limits = value;
                }
            }

            hope help for you

             

            • 3. Re: Databinding
              ckdkdkdlkdjfkljlkmvjklj Level 1

              This does not matter actually, the limits are updated when I "load" the alarm so

              that is not the issue. It is the tag that does not change at runtime.

              • 4. Re: Databinding
                huangxinghui Level 1

                sorry, i misunderstand.

                 

                 

                the code change like this:

                 

                public function onClick(evt:Event):void 
                {  
                    myAlarm.loadAlarm(newName);
                    var arr:ArrayCollection = ArrayCollection(alarmlist.dataProvider);
                    arr.itemUpdated(arr.getItemAt(0));
                    trace(myAlarm._tag);
                }
                

                 

                use itemUpdated method to update the view

                 

                Thanks

                1 person found this helpful
                • 5. Re: Databinding
                  ckdkdkdlkdjfkljlkmvjklj Level 1

                  Hi !

                   

                  First of all thanks, this indeed works fine !

                  But..

                  I also have some charts and components that have to

                  be updated to. Now I can fix some of them with the solution you gave me.

                  But to put everything in a ArrayCollection, is that really the best way ? Because it

                  looks to me like a work around

                  Maybe I can make an itemUpdated method for my actionscript object ?

                   

                  Also some of the dataProviders are functions of my object that return arrays or custom objects..

                  You can not change them into an ArrayCollection (compiler complained) so for those things, the solution does not work.

                  • 6. Re: Databinding
                    huangxinghui Level 1

                    may be you should not use binding, just set the dataProviders directly

                     

                    http://livedocs.adobe.com/flex/3/html/help.html?content=databinding_8.html

                    explain

                    the bindable metatag

                     

                    hope help for you

                     

                    2011/3/18 ckdkdkdlkdjfkljlkmvjklj <forums@adobe.com>

                     

                    Hi !

                    >

                    First of all thanks, this indeed works fine !

                    But..

                    I also have some charts and components that have to

                    be updated to. Now I can fix some of them with the solution you gave me.

                    But to put everything in a ArrayCollection, is that really the best way ?

                    Because it

                    looks to me like a work around

                    >

                    Also some of the dataProviders are functions of my object that return

                    arrays or custom objects..

                    You can not change them into an ArrayCollection (compiler complained) so

                    for those things, the solution does not work.

                    >

                    • 7. Re: Databinding
                      ckdkdkdlkdjfkljlkmvjklj Level 1

                      I fixed the issue by making the loadAlarm function a static one that returns a new Alarm object, instead of just changing the Alarm object. Because then the address of myAlarm remains the same so the dataBinding does not generates the necessary events..

                       

                      Many thanks for the ideas !