0 Replies Latest reply on May 18, 2009 10:44 PM by Randy Croucher

    Binding best practices

    Randy Croucher

      Hi, I am relatively new to Flex.  I have an app up and running that is getting fairly large, but every time I run into this same type of issue I ask myself "Can I really be doing this the best way, there must be a standard way to pull this off".

       

      Essentially, I would like to know the best way to have controls that are bound to a getter get notified when that getter changes value.  The key is that the getter computes a value by using both getters and member variables from other classes.  If any of the inputs change, the output value from the getter changes as well.  What is the property way to set this chain of events up?

       

      For example,

      public class LevelScore
      {
           public var level:int;
           public var points:int;
      }
      
      public class Game
      
      {
           private var levelScoreArray:ArrayCollection = new ArrayCollection();
      
           public function get totalScore():int
           {
                var total:int = 0;
      
                for each (var levelScore:LevelScore in levelScoreArray)
                {
                     total += levelScore.points;
                }
      
                return total;
           }
      }
       
      
      public class Player
      {
           public var handicap:int = 0;
      }
       
      
      public class MyClass
      {
           public var player:Player;
           public var game:Game;
       
           public function get handicappedScore():int
           {
                return game.totalScore - player.handicap;
           }
      }
       
       
      
      

       

      (I just made this example up in this post, so it may not compile, it is just to get my point accross)

       

      I have intentionally left out any [Bindable] tags, bindSetters, bindProperties, dispatchEvents etc. because I am afraid I am abusing the system the way I normally pull it off.  And I find myself adding stuff until it seems to work, and never fully understanding it.

       

      So given the above example, lets say we have a text field with access to a MyClass variable called myClass and it needs to display its handicappedScore by setting it's text field to {myClass.handicappedScore}.  Now in order for that text to always be correct, it is going to need to recall the handicappedScore getter if any of the associated LevelScores change (which should cause game.totalScore to change), if the Player or Game changes, or if the Player's Handicap changes.  Of course other controls will probably be bound to any of the stuff in between as well (like Game.totalScore).

       

      Any pointers would be appreciated, and thanks in advance for even making it this far through this long post..

       

      -Randy