Binding - Radios
Introduction
- A group of radio inputs is a type of "single select" interface.
- Aurelia supports two-way binding any type of property to a group of radio inputs.
- The examples below illustrate binding number, object, string and boolean properties to sets of radio inputs.
- In each of the examples there's a common set of steps:
- Group the radios via the name property. Radio buttons that have the same value for the name attribute are in the same "radio button group"; only one radio button in a group can be selected at a time.
Define each radio's value using the model property.
Two-way bind each radio's checked attribute to a "selected item" property on the view-model.
- Group the radios via the name property. Radio buttons that have the same value for the name attribute are in the same "radio button group"; only one radio button in a group can be selected at a time.
Define each radio's value using the model property.
Two-way bind each radio's checked attribute to a "selected item" property on the view-model.
Numbers
- uses a numeric "selected item" property.
- each radio input will be assigned a number value via the model property.
- Selecting a radio will cause it's model value to be assigned to the selectedProductId property.
Objects
- The binding system supports binding all types to radios, including objects
Objects with Matcher
- You may run into situations where the object your input element's model is bound to does not have reference equality to any of the object in your checked attribute is bound to.
- The objects might match by id, but they may not be the same object instance.
- To support this scenario you can override Aurelia's default "matcher" which is a equality comparison function that looks like this: (a, b) => a === b.
- You can substitute a function of your choosing that has the right logic to compare your objects.
Strings
- does not use model.bind to assign each radio's value.
- Instead the input's standard value attribute is used.
- Normally we cannot use the standard value attribute in conjunction with checked binding because it coerces anything it's assigned to a string.
Comments
Post a Comment