2-Way Data Binding
The view adapter engine supports binding models to view adapters by way of an intermediary known as anobserver
. Models are wrapped by anobserver
and then theobserver
instance is "bound" to the view adapters in a hierarchal fashion. When changes are sent through theobserver
to the model, theobserver
notifies bound listeners of the changes. During the binding, theobserver
monitors which properties the listener is getting values from and binds the listeners to each observed property. This check is re-performed each time a listener is updated due to a change in a property value.
If the view adapter is a container, when anobserver
is bound, the view adapter binds to the observed model and forwards the binding to all of it's child controls. If the view adapter is a repeater, when anobserver
is bound, the view adapter binds to and iterates through the observed array and creates a cloned instance of it's item control template for each item in the array and binds the clone to the item. If the view adapter is an input type control, when anobserver
is bound, the view adapter 2 way binds itself to the observed model.
You can access a value from the observed model using standard object notation. If the value located at a particular path is either an object or an array, then the value returned will be wrapped in anobserver
. To access the natural value contained within theobserver
, simply call the returnobserver
with no path specified. The following calls on an observed model are all valid examples:
function(observer)
{
var model =
new observer
({
someInt: 1,
someObject:
{
someArray:
[
{
someOtherInt: 2
}
]
}
});
// returns an integer value 1
var value = model("someInt");
// returns an integer value 2
value = model("someObject.someArray[0].someOtherInt");
// returns an observed object { someOtherInt: 2 }
value = model("someObject.someArray[0]");
// returns an object { someOtherInt: 2}
value = model("someObject.someArray[0]")();
// returns an observed array [{ someOtherInt: 2 }]
value = model("someObject.someArray");
// returns an array [{ someOtherInt: 2 }]
value = model("someObject.someArray")();
// returns an observed object { someArray: [{ someOtherInt: 2 }] }
value = model("someObject");
// returns an object { someArray: [{ someOtherInt: 2 }] }
value = model("someObject")();
// returns an object { someInt: 1, someObject: { someArray: [{ someOtherInt: 2 }] } }
value = model();
}