Bindable setter patterns
Use bindable property setters to transform incoming values before Aurelia applies them.
In some cases, you want to make an impact on the value that is binding. For such a scenario you can use the possibility of new set.
@bindable({
set: value => someFunction(value), /* HERE */
// Or set: value => value,
mode: /* ... */
})Suppose you have a carousel component in which you want to enable navigator feature for it. You probably imagine such a thing for yourself.
<!-- Enable -->
<my-carousel navigator.bind="true">
<my-carousel navigator="true">
<my-carousel navigator=true>
<my-carousel navigator>
<!-- Disable -->
<my-carousel navigator.bind="false">
<my-carousel navigator="false">
<my-carousel navigator=false>
<my-carousel>In version two, you can easily implement such a capability with the set feature.
To make things easier, first design a new type that accepts true and false as a string and a boolean.
Define your property like this:
For set part, we need functionality to check the input, If the value is one of the following, we want to return true, otherwise we return the false value.
'': No input for a standalonenavigatorproperty.true: When thenavigatorproperty set totrue."true": When thenavigatorproperty set to"true".
So our function will be like this
Now, we should set truthyDetector function as following:
Although, there is another way to write the functionality too
You can simply use any of the above four methods to enable/disable your feature.
Last updated
Was this helpful?