|
4 | 4 |
|
5 | 5 | > :point_right: Make sure to first read the [introduction to data binding](./index.md). |
6 | 6 |
|
7 | | -This decorator creates two-way bindings within a custom component. Changes to the decorated *component property* are reflected on the *target property* of a *target element* (child) and the other way around. |
| 7 | +This decorator creates two-way bindings within a custom component. Changes to the decorated *component property* value are reflected on the *target property* of a *target element* (child) and the other way around. |
8 | 8 |
|
9 | 9 | `@bind` can by applied to any property of a class decorated with [`@component`](./@component.md). It also implies [`@property`](./@property.md) and includes its [typeGuard](./@property.md) feature. Only one of the two can be applied to the same property. |
10 | 10 |
|
11 | | -`@bind` requires exactly one parameter: |
| 11 | +`@bind` has several signatures: |
12 | 12 |
|
13 | | -## @bind(options) |
| 13 | +## @bind({path: string, typeGuard?: Function}) |
| 14 | + |
| 15 | +Where `path` has the format `'#<targetElementId>.<targetProperty>'`. |
| 16 | + |
| 17 | +Binds the decorated *component property* to the property `<targetProperty>` of the *target element* (descendant widget) with an `id` of `<targetElementId>`. Example: |
14 | 18 |
|
15 | | -Where `options` is of the type |
16 | 19 | ```ts |
17 | | -{ |
18 | | - path: "#<targetElementId>.<targetProperty>", |
19 | | - typeGuard?: Function |
20 | | -} |
| 20 | +@bind({path: '#source.selection'}) |
| 21 | +public myNumber: number = 50; |
21 | 22 | ``` |
22 | 23 |
|
23 | | -> See example app ["bind-two-way"](../../examples/bind-two-way). |
| 24 | +This establishes a two-way binding from the `myNumber` property to the property `selection` of the child with the id `'source'`. The binding is established after `append` is called the first time on the component, there needs to be exactly one descendant widget with the given id, and it has to have a property of the same type. |
24 | 25 |
|
25 | | -Binds the decorated *component property* to the property `<targetProperty>` of the *target element* (descendant widget) with an `id` of `<targetElementId>`. The binding is established after `append` is called the first time on the component, there needs to be exactly one descendant widget with the given id, and it has to have a property of the same type. |
| 26 | +> See example app ["bind-two-way"](../../examples/bind-two-way). |
26 | 27 |
|
27 | | -Change events are fired for the decorated *component property* if (and only if) the *target element* fires change events. |
| 28 | +Change events are fired for the decorated *component property* when the *target element* fires change events. |
28 | 29 |
|
29 | 30 | > See example app ["bind-two-way-change-events"](../../examples/bind-two-way-change-events). |
30 | 31 |
|
31 | | -A [`typeGuard`](./@property.md#propertytypeguard). may be given to perform value checks. |
| 32 | +A [`typeGuard`](./@property.md#propertytypeguard) may be given to perform value checks. |
32 | 33 |
|
33 | | -As with one-way bindings, setting the *component property* to `undefined` resets the *target property* to its initial value. |
| 34 | +As with one-way bindings, setting the *component property* to `undefined` resets the *target property* to its initial value for when the binding was first established. |
34 | 35 |
|
35 | 36 | ## @bind(path) |
36 | 37 |
|
37 | | -Shorthand for `@bind({path: path})` |
| 38 | +Shorthand for `@bind({path: path})`. |
| 39 | + |
| 40 | +Example: |
| 41 | + |
| 42 | +```ts |
| 43 | +@bind('#source.selection') |
| 44 | +public myNumber: number = 50; |
| 45 | +``` |
| 46 | + |
| 47 | +## @bind({all: Bindings, typeGuard?: Function}) |
| 48 | + |
| 49 | +Where `Bindings` is in the format of: |
| 50 | +``` |
| 51 | +{ |
| 52 | + <sourceProperty>: '#<targetElementId>.<targetProperty>' |
| 53 | +} |
| 54 | +``` |
| 55 | +Establish a two-way binding between the property `<sourceProperty>` of the *object assigned to the decorated property* and the property `<targetProperty>` of the *target element* (descendant widget) with an `id` of `<targetElementId>`. Multiple bindings may be established this way. Example: |
| 56 | + |
| 57 | +```ts |
| 58 | +@bind({all:{ |
| 59 | + myText: '#input1.text', |
| 60 | + myNumber: '#input2.selection' |
| 61 | +}}) |
| 62 | +public model: Model; |
| 63 | +``` |
| 64 | + |
| 65 | +This establishes 2 two-way bindings: |
| 66 | +* One between the `myText` property of the assigned `Model` object and the property `text` of the child with the id `input1`. |
| 67 | +* And one between the `myNumber` property of the assigned `Model` object and the property `selection` of the child with the id `input2`. |
| 68 | + |
| 69 | +> See example app ["bind-two-way-model"](../../examples/bind-two-way-model). |
| 70 | +
|
| 71 | +The bindings are first established when `append` is called the first time on the component. Again, the bindings are established after `append` is called the first time on the component, there needs to be exactly one descendant widget with the given id for each binding, and they have to have a property of the same type as the source property. |
| 72 | + |
| 73 | +The `model` property can be set at any time and the bindings will update accordingly. However, the target elements will always stay the same. |
| 74 | + |
| 75 | +See also [`@bindAll`](./@bindAll.md). |
| 76 | + |
| 77 | +### Properties eligible for bindings |
| 78 | + |
| 79 | +Both source and target property need to generate change events for the two-way binding to work. The quickest way to implement this is using [`@property`](./@property.md), which can be used on non-widget classes as well: |
| 80 | + |
| 81 | +```ts |
| 82 | +class Model { |
| 83 | + @property public myText: string; |
| 84 | + @property public myNumber: number; |
| 85 | +} |
| 86 | +``` |
| 87 | + |
| 88 | +Note that there is no need to explicitly create an event API, `@bind` can 'talk' directly to `@property`. However, an explicit implementation is also possible: |
| 89 | + |
| 90 | +```ts |
| 91 | +class Model { |
| 92 | + |
| 93 | + @event public onMyTextChanged: ChangeListeners<Model, 'myText'>; |
| 94 | + private _myText: string; |
| 95 | + |
| 96 | + public set myText(value: string) { |
| 97 | + if (this._myText !== value) { |
| 98 | + this._myText = value; |
| 99 | + this.onMyTextChanged.trigger({value}); |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + public get myText() { |
| 104 | + return this._myText; |
| 105 | + } |
| 106 | + |
| 107 | +} |
| 108 | +``` |
| 109 | + |
| 110 | +### Edge Cases |
| 111 | + |
| 112 | +The component property (`model` in the above example) may also be set to `null` (or `undefined`) at any time. In that case all the target properties will be set back to their initial values. The initial value in this case refers to the value a target property had the moment the target element was attached to the component. |
| 113 | + |
| 114 | +When a new two-way binding is established all the target properties will be set to the current value of the their respective source property. There is one exception to this behavior: If the source property is set to `undefined` (but not `null`) at that moment it will be assigned the current value of the target property. If a source property is set to `undefined` later both properties are set to the initial value of the target property. |
| 115 | + |
| 116 | +If a source property converts or ignores the incoming value of the target property, the target property will follow and change again to contain the new source property value. |
| 117 | + |
| 118 | +If a target property converts or ignores the incoming value of a source property, the source property will ignore that and keep its own value. The two properties are out-of-sync in this case. |
| 119 | + |
| 120 | +If either property throws, the error will be propagated to the caller that originally caused the value change. In this case the two properties *may* end up out-of-sync. |
0 commit comments