Bugs: One good thing about them is they keep on inspiring even when you don't want to be.

Friday, February 10, 2012

Binding (Part 2)

Question: How to use bi-directional
binding.

Answer:We can define a
bidirectional data binding using one of the following methods:

1. Define two objects that specify as the source a property of
the other object. In the following example, input1 specifies input2.text as the
source property, and input2 specifies input1.text as the source property. Any
change to input1.text updates input2.text, and any change to input2.text
updates input1.text:

Note: The property definition that includes
the @{bindable_property} syntax i s called the primary property.
If the primary property has not had a value assigned to it, the binding to it
occurs first, before a binding to the other property.

3. Use the twoWay property of the tag, as the
following example shows:

We use the [Bindable]metadata tag to specify to Flex to invoke the isEnabled()function in
response to the event myFlagChanged. When the myFlag setter gets called, it dispatches the myFlagChangedevent to trigger
any data bindings that use the isEnabled()function as the source:

Question: Give example of using data
binding with objects.

Answer: There is a difference
between binding to an object or binding to properties of an object.

Answer:When working with
arrays, such as Array or ArrayCollection objects, we can define the array as
source or destination of binding expression.

Note: When defining a data binding expression
that uses an array as the source of a data binding expression, the array should
be of type ArrayCollection because the ArrayCollection class dispatches an
event when the array or the array elements change to trigger data binding. For
example, a call to ArrayCollection.addItem(),ArrayCollection.addItemAt(), ArrayCollection.removeItem(), and ArrayCollection.removeItemAt()all trigger data binding.

If you specify an array element as the source of a data
binding expression by using the square bracket syntax, [], data binding is only
triggered when the application starts and when the array or a reference to the
array is updated; data binding is not triggered when the individual array
element is updated. The compiler issues a warning in this situation. However,
the data binding expression myAC.getItemAt(0)is triggered when an array element
changes. Therefore, the text2 Text control is updated when we click button1,
while text1 is not. When using an array element as the source of a data binding
expression, you should use the ArrayCollection.getItemAt()method in the binding expression.
Clicking button2 copies myAC2 to myAC, and triggers all data bindings to array
elements regardless of how you implemented them.

Question: What are the difference
between binding at compile time (using mxml) and binding at runtime (using
Action script)?

Answer:1. We cant use any action script code in bindProperty or bindSetter() method. Instead bindSetter() specify a method to
call when binding occurs.

2. We cannot include
an E4X expression in data binding expression in AS.

3. We cannot include
functions or array elements in property chains in a data binding expressions

defined by
bindProperty() or bindSetter() method.

4. MXML compiler has
better warning and error detection support than runtime data binding by