Reading through some section of Aurelia docs, you will see some code like this (not exactly the same)
<!-- https://aurelia.io/docs/binding/checkboxes#array-of-objects -->
<input type="checkbox" model.bind="item" checked.bind="selectedItem" />
<!-- or -->
<!-- https://aurelia.io/docs/binding/radios#objects -->
<input type="radio" model.bind="item" checked.bind="selectedItem" />
<!-- or -->
<!-- https://aurelia.io/docs/binding/selects#select-object -->
<select value.bind="selectedItem">
<option repeat.for="item of activeItems"
model.bind="item">${item.name}</option>
</select>
You may wonder what model.bind
is and why it is needed. Simple answer is: model.bind
is a way to associate a value that is not a string to <input/>
or <option/>
element, as <input/>
and <option/>
element always convert to string whatever value you assign to their value
property. You can test its behavior via copy pasting the following block into your browser console:
let i = document.createElement('input');
i.value = {};
console.log(i.value) // [object Object]
let o = document.createElement('option');
o.value = {};
console.log(o.value) // [object Object]
In two way binding scenarios, Aurelia binding system, upon input event from those <input/>
or <select/>
elements, will get the value of those elements via element.value
and update your view model. And as explained above, property value
of <input/>
and <option/>
are quite problematic when it comes to anything that is not a string. This is where model
property is needed. Aurelia will check if model
exists and get value from it before it fallback to value
property. By having a model.bind
binding on those element, a model
property will be created, and it can store any value ie. object, array, number…, so you can bind it to anything without worrying about coercion.