1# \@Observed and \@ObjectLink Decorators: Observing Attribute Changes in Nested Class Objects 2 3 4The decorators described above can observe only the changes of the first layer. However, in real-world application development, the application may encapsulate its own data model based on development requirements. In the case of multi-layer nesting, for example, a two-dimensional array, an array item class, or a class insider another class as an attribute, the attribute changes at the second layer cannot be observed. This is where the \@Observed and \@ObjectLink decorators come in handy. 5 6 7> **NOTE** 8> 9> Since API version 9, these two decorators are supported in ArkTS widgets. 10 11 12## Overview 13 14\@ObjectLink and \@Observed class decorators are used for two-way data synchronization in scenarios involving nested objects or arrays: 15 16- Regarding classes decorated by \@Observed, the attribute changes can be observed. 17 18- The \@ObjectLink decorated state variable in the child component is used to accept the instance of the \@Observed decorated class and establish two-way data binding with the corresponding state variable in the parent component. The instance can be an \@Observed decorated item in the array or an \@Observed decorated attribute in the class object. 19 20- Using \@Observed alone has no effect. Combined use with \@ObjectLink for two-way synchronization or with [\@Prop](arkts-prop.md) for one-way synchronization is required. 21 22 23## Restrictions 24 25Using \@Observed to decorate a class changes the original prototype chain of the class. Using \@Observed and other class decorators to decorate the same class may cause problems. 26 27## Decorator Description 28 29| \@Observed Decorator| Description | 30| -------------- | --------------------------------- | 31| Decorator parameters | None. | 32| Class decorator | Decorates a class. You must use **new** to create a class object before defining the class.| 33 34| \@ObjectLink Decorator| Description | 35| ----------------- | ---------------------------------------- | 36| Decorator parameters | None. | 37| Synchronization type | No synchronization with the parent component. | 38| Allowed variable types | Objects of \@Observed decorated classes. The type must be specified.<br>Simple type variables are not supported. Use [\@Prop](arkts-prop.md) instead.<br>An \@ObjectLink decorated variable accepts changes to its attributes, but assignment is not allowed. In other words, an \@ObjectLink decorated variable is read-only and cannot be changed.| 39| Initial value for the decorated variable | Not allowed. | 40 41Example of a read-only \@ObjectLink decorated variable: 42 43 44```ts 45// The \@ObjectLink decorated variable accepts changes to its attribute. 46this.objLink.a= ... 47// Value assignment is not allowed for the \@ObjectLink decorated variable. 48this.objLink= ... 49``` 50 51> **NOTE** 52> 53> Value assignment is not allowed for the \@ObjectLink decorated variable. To assign a value, use [@Prop](arkts-prop.md) instead. 54> 55> - \@Prop creates a one-way synchronization from the data source to the decorated variable. It takes a copy of its source tp enable changes to remain local. When \@Prop observes a change to its source, the local value of the \@Prop decorated variable is overwritten. 56> 57> - \@ObjectLink creates a two-way synchronization between the data source and the decorated variable. An \@ObjectLink decorated variable can be considered as a pointer to the source object inside the parent component. If value assignment of an \@ObjectLink decorated variable occurs, the synchronization chain is interrupted. 58 59 60## Variable Transfer/Access Rules 61 62| \@ObjectLink Transfer/Access| Description | 63| ----------------- | ---------------------------------------- | 64| Initialization from the parent component | Mandatory.<br>To initialize an \@ObjectLink decorated variable, a variable in the parent component must meet all the following conditions:<br>- The variable type is an \@Observed decorated class.<br>- The initialized value must be an array item or a class attribute.<br>- The class or array of the synchronization source must be decorated by \@State, \@Link, \@Provide, \@Consume, or \@ObjectLink.<br>For an example where the synchronization source is an array item, see [Object Array](#object-array). For an example of the initialized class, see [Nested Object](#nested-object).| 65| Synchronize with the source | Two-way. | 66| Subnode initialization | Supported; can be used to initialize a regular variable or \@State, \@Link, \@Prop, or \@Provide decorated variable in the child component.| 67 68 69 **Figure 1** Initialization rule 70 71 72 73 74 75## Observed Changes and Behavior 76 77 78### Observed Changes 79 80If the attribute of an \@Observed decorated class is not of the simple type, such as class, object, or array, it must be decorated by \@Observed. Otherwise, the attribute changes cannot be observed. 81 82 83```ts 84class ClassA { 85 public c: number; 86 87 constructor(c: number) { 88 this.c = c; 89 } 90} 91 92@Observed 93class ClassB { 94 public a: ClassA; 95 public b: number; 96 97 constructor(a: ClassA, b: number) { 98 this.a = a; 99 this.b = b; 100 } 101} 102``` 103 104In the preceding example, **ClassB** is decorated by \@Observed, and the value changes of its member variables can be observed. In contrast, **ClassA** is not decorated by \@Observed, and therefore its attribute changes cannot be observed. 105 106 107```ts 108@ObjectLink b: ClassB 109 110// The value assignment can be observed. 111this.b.a = new ClassA(5) 112this.b.b = 5 113 114// ClassA is not decorated by @Observed, and its attribute changes cannot be observed. 115this.b.a.c = 5 116``` 117 118\@ObjectLink: \@ObjectLink can only accept instances of classes decorated by \@Observed. The following can be observed: 119 120- Value changes of the attributes that **Object.keys(observedObject)** returns. For details, see [Nested Object](#nested-object). 121 122- Replacement of array items for the data source of an array and changes of class attributes for the data source of a class. For details, see [Object Array](#object-array). 123 124 125### Framework Behavior 126 1271. Initial render: 128 1. \@Observed causes all instances of the decorated class to be wrapped with an opaque proxy object, which takes over the setter and getter methods of the attributes on the class. 129 2. The \@ObjectLink decorated variable in the child component is initialized from the parent component and accepts the instance of the \@Observed decorated class. The \@ObjectLink decorated wrapped object registers itself with the \@Observed decorated class. 130 1312. Attribute update: When the attribute of the \@Observed decorated class is updated, the system uses the setter and getter of the proxy, traverses the \@ObjectLink decorated wrapped objects that depend on it, and notifies the data update. 132 133 134## Application Scenarios 135 136 137### Nested Object 138 139The following is the data structure of a nested class object. 140 141 142```ts 143// objectLinkNestedObjects.ets 144let NextID: number = 1; 145 146@Observed 147class ClassA { 148 public id: number; 149 public c: number; 150 151 constructor(c: number) { 152 this.id = NextID++; 153 this.c = c; 154 } 155} 156 157@Observed 158class ClassB { 159 public a: ClassA; 160 161 constructor(a: ClassA) { 162 this.a = a; 163 } 164} 165``` 166 167 168 The following component hierarchy presents this data structure. 169 170```ts 171@Component 172struct ViewA { 173 label: string = 'ViewA1'; 174 @ObjectLink a: ClassA; 175 176 build() { 177 Row() { 178 Button(`ViewA [${this.label}] this.a.c=${this.a.c} +1`) 179 .onClick(() => { 180 this.a.c += 1; 181 }) 182 } 183 } 184} 185 186@Entry 187@Component 188struct ViewB { 189 @State b: ClassB = new ClassB(new ClassA(0)); 190 191 build() { 192 Column() { 193 ViewA({ label: 'ViewA #1', a: this.b.a }) 194 ViewA({ label: 'ViewA #2', a: this.b.a }) 195 196 Button(`ViewB: this.b.a.c+= 1`) 197 .onClick(() => { 198 this.b.a.c += 1; 199 }) 200 Button(`ViewB: this.b.a = new ClassA(0)`) 201 .onClick(() => { 202 this.b.a = new ClassA(0); 203 }) 204 Button(`ViewB: this.b = new ClassB(ClassA(0))`) 205 .onClick(() => { 206 this.b = new ClassB(new ClassA(0)); 207 }) 208 } 209 } 210} 211``` 212 213 214Event handlers in **ViewB**: 215 216 217- this.b.a = new ClassA(0) and this.b = new ClassB(new ClassA(0)): Change to the \@State decorated variable **b** and its attributes. 218 219- this.b.a.c = ... : Change at the second layer. Though [@State](arkts-state.md#observed-changes) cannot observe the change at the second layer, the change of an attribute of \@Observed decorated ClassA, which is attribute **c** in this example, can be observed by \@ObjectLink. 220 221 222Event handlers in **ViewA**: 223 224 225- this.a.c += 1: Changes to the \@ObjectLink decorated variable cause the button label to be updated. Unlike \@Prop, \@ObjectLink does not have a copy of its source. Instead, \@ObjectLink creates a reference to its source. 226 227- The \@ObjectLink decorated variable is read-only. Assigning **this.a = new ClassA(...)** is not allowed. Once value assignment occurs, the reference to the data source is reset and the synchronization is interrupted. 228 229 230### Object Array 231 232An object array is a frequently used data structure. The following example shows the usage of array objects. 233 234 235```ts 236@Component 237struct ViewA { 238 // The type of @ObjectLink of the child component ViewA is ClassA. 239 @ObjectLink a: ClassA; 240 label: string = 'ViewA1'; 241 242 build() { 243 Row() { 244 Button(`ViewA [${this.label}] this.a.c = ${this.a.c} +1`) 245 .onClick(() => { 246 this.a.c += 1; 247 }) 248 } 249 } 250} 251 252@Entry 253@Component 254struct ViewB { 255 // ViewB has the @State decorated ClassA[]. 256 @State arrA: ClassA[] = [new ClassA(0), new ClassA(0)]; 257 258 build() { 259 Column() { 260 ForEach(this.arrA, 261 (item) => { 262 ViewA({ label: `#${item.id}`, a: item }) 263 }, 264 (item) => item.id.toString() 265 ) 266 // Initialize the @ObjectLink decorated variable using the array item in the @State decorated array, which is an instance of ClassA decorated by @Observed. 267 ViewA({ label: `ViewA this.arrA[first]`, a: this.arrA[0] }) 268 ViewA({ label: `ViewA this.arrA[last]`, a: this.arrA[this.arrA.length-1] }) 269 270 Button(`ViewB: reset array`) 271 .onClick(() => { 272 this.arrA = [new ClassA(0), new ClassA(0)]; 273 }) 274 Button(`ViewB: push`) 275 .onClick(() => { 276 this.arrA.push(new ClassA(0)) 277 }) 278 Button(`ViewB: shift`) 279 .onClick(() => { 280 this.arrA.shift() 281 }) 282 Button(`ViewB: chg item property in middle`) 283 .onClick(() => { 284 this.arrA[Math.floor(this.arrA.length / 2)].c = 10; 285 }) 286 Button(`ViewB: chg item property in middle`) 287 .onClick(() => { 288 this.arrA[Math.floor(this.arrA.length / 2)] = new ClassA(11); 289 }) 290 } 291 } 292} 293``` 294 295- this.arrA[Math.floor(this.arrA.length/2)] = new ClassA(..): The change of this state variable triggers two updates. 296 1. ForEach: The value assignment of the array item causes the change of [itemGenerator](arkts-rendering-control-foreach.md#api-description) of **ForEach**. Therefore, the array item is identified as changed, and the item builder of ForEach is executed to create a **ViewA** component instance. 297 2. ViewA({ label: ViewA this.arrA[first], a: this.arrA[0] }): The preceding update changes the first element in the array. Therefore, the **ViewA** component instance bound to **this.arrA[0]** is updated. 298 299- this.arrA.push(new ClassA(0)): The change of this state variable triggers two updates with different effects. 300 1. ForEach: The newly added Class A object is unknown to the **ForEach** [itemGenerator](arkts-rendering-control-foreach.md#api-description). The item builder of **ForEach** will be executed to create a **View A** component instance. 301 2. ViewA({ label: ViewA this.arrA[last], a: this.arrA[this.arrA.length-1] }): The last item of the array is changed. As a result, the second **View A** component instance is changed. For **ViewA({ label: ViewA this.arrA[first], a: this.arrA[0] })**, a change to the array does not trigger a change to the array item, so the first **View A** component instance is not refreshed. 302 303- this.arrA[Math.floor (this.arrA.length/2)].c: [@State](arkts-state.md#observed-changes) cannot observe changes at the second layer. However, as **ClassA** is decorated by \@Observed, the change of its attributes will be observed by \@ObjectLink. 304 305 306### Two-Dimensional Array 307 308@Observed class decoration is required for a two-dimensional array. You can declare an \@Observed decorated class that extends from **Array**. 309 310 311```ts 312@Observed 313class StringArray extends Array<String> { 314} 315``` 316 317 318 319Declare a class that extends from **Array**: **class StringArray extends Array\<String> {}** and create an instance of **StringArray**. The use of the **new** operator is required for the \@Observed class decorator to work properly. 320 321 322```ts 323@Observed 324class StringArray extends Array<String> { 325} 326 327@Component 328struct ItemPage { 329 @ObjectLink itemArr: StringArray; 330 331 build() { 332 Row() { 333 Text('ItemPage') 334 .width(100).height(100) 335 336 ForEach(this.itemArr, 337 item => { 338 Text(item) 339 .width(100).height(100) 340 }, 341 item => item 342 ) 343 } 344 } 345} 346 347@Entry 348@Component 349struct IndexPage { 350 @State arr: Array<StringArray> = [new StringArray(), new StringArray(), new StringArray()]; 351 352 build() { 353 Column() { 354 ItemPage({ itemArr: this.arr[0] }) 355 ItemPage({ itemArr: this.arr[1] }) 356 ItemPage({ itemArr: this.arr[2] }) 357 358 Divider() 359 360 ForEach(this.arr, 361 itemArr => { 362 ItemPage({ itemArr: itemArr }) 363 }, 364 itemArr => itemArr[0] 365 ) 366 367 Divider() 368 369 Button('update') 370 .onClick(() => { 371 console.error('Update all items in arr'); 372 if (this.arr[0][0] !== undefined) { 373 // We should have a real ID to use with ForEach, but we do no. 374 // Therefore, we need to make sure the pushed strings are unique. 375 this.arr[0].push(`${this.arr[0].slice(-1).pop()}${this.arr[0].slice(-1).pop()}`); 376 this.arr[1].push(`${this.arr[1].slice(-1).pop()}${this.arr[1].slice(-1).pop()}`); 377 this.arr[2].push(`${this.arr[2].slice(-1).pop()}${this.arr[2].slice(-1).pop()}`); 378 } else { 379 this.arr[0].push('Hello'); 380 this.arr[1].push('World'); 381 this.arr[2].push('!'); 382 } 383 }) 384 } 385 } 386} 387``` 388