Bidirectional (two-way) data binding in Flex 4
I use data binding a lot when writing Flex applications. One of the new features in Flex 4 is bidirectional, or two-way data binding. This blog post shows you a simple example that demonstrates the difference between one-way and two-way data binding syntax.
The first example is the most commonly used data binding scenario. It uses the curly braces syntax. In the example a String variable myName binds to a TextInput field. However if you change the value in the input field, the underlying String variable is not changed. This is because this kind of data binding is only one-way. There is a way to achieve bidirectional data binding in Flex 3 and you can find more info in this article, but I will not go into detail about that in this post.
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml">
<mx:String id="myName">Alain</mx:String>
<mx:TextInput id="nameInput" text="{myName}"/>
</mx:Application>
For example in Flex 3 the DataGrid component had two-way data binding enabled if you made it editable. In Felx 4 it has become very easy to achieve bidirectional binding. The following example is based on the same scenario as the previous one, only now the underlying String value is updated.
<s:Application
xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark">
<fx:Declarations>
<fx:String id="myName"/>
</fx:Declarations>
<s:layout>
<s:VerticalLayout/>
</s:layout>
<s:TextInput id="nameInput" text="@{myName}"/>
<s:SimpleText text="{myName}"/>
</s:Application>
That’s basically it. The @{} syntax makes sure that the myName variable is changed and because the SimpleText label is also binded to the myName variable the value is also updated.
These are pretty simple examples, but you can find more information about the Flex 4 data binding techniques in the following articles:
2 Responses to Bidirectional (two-way) data binding in Flex 4
Leave a Reply Cancel reply
Lifestream
-
Checking out CocoaPods - an Objective-C library manager > https://t.co/xdLU5OgO— February 1st via Twitter
-
Finally found some time to dive into iOS5 Storyboarding.— February 1st via Twitter
-
RT @robbedoes: Fun to watch: an interview w/ the sound designer of Angry Birds: http://t.co/lDARIGbB— February 1st via Twitter
-
@wuotr Also, check this out: Flood Fill > http://t.co/SpweLK0N ;)— January 30th via Twitter
-
Love it! Timesaver++ CocosBuilder, a tool for graphically laying out sprites, layers and scenes for @cocos2d > http://t.co/Jrb8233D— January 30th via Twitter
-
RT @rwenderlich: Awesome list of Cocos2D source code projects, extensions and code from @iuridium: http://t.co/ovwLYhCv— January 29th via Twitter
-
Moo! @ Kinderboerderij kiewit http://t.co/tYBlLsvH— January 29th via Twitter
-
I've been doing my epic chores and just leveled-up. I'm a Level 8 Well-groomed Clansman now! #EpicWinApp— January 27th via Twitter
-





Blogged: Bidirectional (two-way) data binding in Flex 4 http://bit.ly/15AGVE (one of my favorite new features)
This comment was originally posted on Twitter
Pointers in your flex !
But yeah, pretty cool, that should eliminate quite a bit of boilerplate code.