Tuesday, May 4, 2010

Flex Databinding

If you have done Flex development you are probably no stranger to databinding. Data binding will update a property any time the bound property is modified. What it is updated with is another story. I will list various ways to use databinding beyond just mapping one value to a property.

One variable:
The variable can exist in the MXML or a class it inherits.
text="{total}"

Or it can be inside an object. If so Flex will detect changes to the value and assignments to any of the objects in the binding.
text="{model.total}"
text="{model.products.total}"

A variable and text:
The property is not limited to only a bound or non bound value. You can insert the binding into a constant.
text="Your total is: {total}"

Multipule variables and logic:
Slightly more advanced we can use more than one value in a binding. In the following example flex will fire the binding if either "a" or "b" is changed.
text="{a} + {b} = {a+b}"
visible="{a || b}"

Functions:
My favorite trick. If the logic is complicated, use data binding to call your function when the variables change. The function will be called any time the variables are changed and the result of that function will be applied to the property defined.
text="{getCustomMessage(itemCount,userName)}"

No comments:

Post a Comment