Two-way Data Binding - Functional and Design SpecificationTwo-way Data Binding - Functional and Design Specification | Glossary | Summary and Background | Usage Scenarios | Detailed Description | API Description | B Features | Examples and Usage | Additional Implementation Details | Prototype Work | Compiler Work | Web Tier Compiler Impact | Flex Feature Dependencies | Backwards Compatibility | Accessibility | Performance | Globalization | Localization | Issues and Recommendations | Documentation
GlossaryData binding is the process of tying the data in one object to another object. It provides a convenient way to pass data between the different layers of the application. Data binding requires a source property, a destination property, and a triggering event that indicates when to copy the data from the source to the destination. An object dispatches the triggering event when the source property changes. Summary and BackgroundTwo-way data binding, aka bidirectional data binding, refers to two components acting as the source object for the destination properties of each other. This is possible to do in Flex 3 using a combination of curly braces, <mx:Binding> statements, and calls to mx.binding.utils.BindingUtils.bindProperty(). This spec introduces a few shorthand ways to accomplish the same thing with fewer steps. This feature has been requested by customers. Usage ScenariosTwo-way data binding is used whenever two properties need to be tied together, for example, two text fields. Detailed DescriptionA two-way data binding expression generates two binding expressions, one with the given source and destination, and the other, with the source and destination reversed. Since the destination must be a bindable property or property chain (reference value), the source must be a bindable property or property chain as well. The source and destination must be the same type. The two ways to specify a two-way data binding are:
In both cases, E4X statements can be used to reference bindable properties. For the <mx:Binding> tag, because both the source and the destination must resolve to a bindable property or property chain, neither can be a one-way or two-way inline binding expression. All the current ways of specifying bindings remain valid. This adds other alternatives to allow two-way bindings to be specified in shorthand. API Description
B Features(Not currently scheduled to be implemented for Flex 4)
The chain would be extended from bindProperty(), to accept an object that contains not just the name of, and a getter function for, but a setter function as well, for a public bindable property of the respective object. // Returns and Array of up to two ChangeWatcher objects. public static function bindPropertyTwoWay( obj1:Object, chain1:Object, obj2:Object, chain2:Object, commitOnly:Boolean = false):Array Examples and UsageIn Flex 3, to implement two-way binding, the code would like the following: <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"> <mx:TextInput id="t1" text="{t2.text}"/> <mx:TextInput id="t2" text="{t1.text}"/> </mx:Application> Using the new shorthand two-way binding syntax this could also be written as: <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"> <mx:TextInput id="t1" text="@{t2.text}"/> <mx:TextInput id="t2"/> </mx:Application> A more useful example would be something like: <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"> <mx:XML id="myWebServiceResult"/> <mx:TextInput id="t1" text="{myWebServiceResult.foo.bar}"/> <mx:Binding source="t1.text" destination="myWebServiceResult.foo.bar"/> </mx:Application> which becomes: <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"> <mx:XML id="myWebServiceResult"/> <mx:TextInput id="t1" text="@{myWebServiceResult.foo.bar}"/> </mx:Application> In Flex 3, if you want to set two-way binding using the <mx:Binding> tag you need to set it twice: <mx:Binding source="a.property" destination="b.property"/> <mx:Binding source="b.property" destination="a.property"/> which becomes:
<mx:Binding source="a.property" destination="b.property" twoWay="true"/>
All of the inline binding expressions below are invalid. <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"> <mx:TextInput id="t1" text="text is @{t2.text}"/> <mx:TextInput id="t2"/> <mx:TextInput id="t3" text="@{t4.text.toUpperCase()}"/> <mx:TextInput id="t4"/> <mx:Binding source="{t2.text}" destination="@{t4.text}" twoWay="true" /> </mx:Application> Additional Implementation DetailsIn order to create a complete BindableExpression, the sourceExpression, the xml line number and the destination lvalue or the destination property must be known. For two-way inline binding, there are also additional binding expression flags which need to be set so that the destination path can be generated properly. Bindable expressions are supported in the many places, not all or which are appropriate for two-way bindable expressions.
Prototype WorkI have prototyped enough of this to figure out how data binding is setup. Compiler Workno additional changes other than described above Web Tier Compiler Impactnone Flex Feature Dependenciesnone Backwards Compatibilityn/a Accessibilityn/a Performancen/a Globalizationnew messages added to appropriate message files LocalizationCompiler Featuresmxml.builder.AbstractBuilder.TwoWayBindingNotAllowedInitializer=Initializer for '${desc}': two-way data binding expression not allowed here. Framework Featuresn/a Issues and RecommendationsCustomer asked
Using the inline notation, it will be possible to bind on E4X statements. Adding support for E4X statements to BindingUtils is another task which needs to be prioritized and scheduled. DocumentationSince this is a new feature it will need to be documented. |
|
| You must be logged in to comment. |
|---|

Comments (5)
Mar 05, 2009
Christophe JOLIF says:
How 2 way binding can work with type conversion. With 1 way I could (can) do: ...How 2 way binding can work with type conversion.
With 1 way I could (can) do:
<?xml version="1.0" encoding="utf-8"?>
<FxApplication xmlns="http://ns.adobe.com/mxml/2009">
<layout>
<VerticalLayout/>
</layout>
<FxHSlider id="slider" value="{Number(field.text)}"/>
<FxTextInput id="field" text="{String(slider.value)}"/>
</FxApplication>
Can we have a syntax to achieve this with 2-way binding? (or there is one and I missed it).
Mar 06, 2009
Matt Chotin says:
This was also a B feature that we're not going to have time to implement. We'd ...This was also a B feature that we're not going to have time to implement. We'd welcome the potential contribution though! We are also thinking about different ways for data to flow through an application as part of our long-term planning, that might be a little more formal than binding, but allow this kind of flexibility.
Jun 21, 2009
s r says:
This topic looks great. For more Flex 4 and Flash Builder 4 information, see th...This topic looks great. For more Flex 4 and Flash Builder 4 information, see this new course 'Flex 4 Top 10 Features' taught by Rivello Multimedia Consulting. Sign up or request it come to your town today! http://www.blog.rivello.org/?p=324
Feb 07, 2010
Lohandus Ribeiro says:
First, sorry for my english. Two-way Data Binding is an incredible feature, but...First, sorry for my english.
Two-way Data Binding is an incredible feature, but as the Christophe Jolife, I stumbled on the variable types problem.
I solved this (at least for me) with the following custom component:
Use:
<my:TextInputNumber number="@{product.prince}" />You can improve this component by adding numerical filter and decimal precision, but for this example is enough.
I have little experience with Flex, so sorry for some bad coding.
Feb 10, 2011
Andy Fleming says:
I battled with this for a while, then came up with this: <s:TextInput id=...I battled with this for a while, then came up with this:
It seems to work, but I'm not sure how proper it is - the custom component solution from Lohandus Ribeiro above is probably nicer.