Data Binding Gone Wild!

October 29, 2008By 6 Comments

The ability to base a property of an object off a dynamically changing value is a benefit of data binding that is often unknown or overlooked by Flex developers. However it is an extremely valuable feature and can save a lot of time coding and handling various events, not to mention much cleaner code! This is particularly true for those properties requiring a boolean, such as ‘enabled’ or ‘visible’. The toggle can be done during execution based on a certain sequence of events. The code returning the value for the property can be included within the binding brackets in the case where minimal code is needed to derive the value, or via a function call that returns the value. The following code excerpt shows how this can easily be done in your Flex application. It basically sets the ‘enable’ property of the Delete button to a dynamically derived value that indicates whether a row in the grid has been clicked. Simple as that!



	
		

	
		
			
				
				
				
				
			
		
		
	

Comments welcome :)

Filed in: Adobe FlexFlex Tags:

About the Author ()

Comments (6)

Trackback URL | Comments RSS Feed

  1. James Ward says:

    Cool stuff! I’ve found that using ternary operators in binding expressions is also very useful. Such as:

    label=”{playing ? ‘Stop’ : ‘Play’}”

    -James

  2. Marcel Boucher says:

    Very good article DevGirl. Data Binding can be extremely effective when you are building applications that require unit testing. Using this approach, you can easily create ActionScript classes that can be detached from UI and tested separately.

    Nice job.

  3. Another tip- If you’re using the same binding expression over and again, put it in a variable, and bind to that.

    Instead of having 5 different components each having enabled=”{ something && somethingElse(foo) }”

    { something && somethingElse(foo) }

    And then your actual GUI components can bind to “{ isBlue }” cutting down on cruft and improving readability.

    You can also replace the MXML definition of “isBlue” with anything else you like down the track (perhaps a [Bindable("isBlueChanged")] get function)…

  4. Another tip- If you’re using the same binding expression over and again, put it in a variable, and bind to that.

    Instead of having 5 different components each having enabled=”{ something && somethingElse(foo) }”

    <mx:Boolean id=”isBlue”>{ something && somethingElse(foo) }</mx:Boolean>

    And then your actual GUI components can bind to “{ isBlue }” cutting down on cruft and improving readability.

    You can also replace the MXML definition of “isBlue” with anything else you like down the track (perhaps a [Bindable("isBlueChanged")] get function)…

Leave a Reply