• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# \@Observed and \@ObjectLink: 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 \@Observeddecorated 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## Decorator Description
24
25| \@Observed Decorator| Description                               |
26| -------------- | --------------------------------- |
27| Decorator parameters         | None.                                |
28| Class decorator          | Decorates a class. You must use **new** to create a class object before defining the class.|
29
30| \@ObjectLink Decorator| Description                                      |
31| ----------------- | ---------------------------------------- |
32| Decorator parameters            | None.                                       |
33| Synchronization type             | No synchronization with the parent component.                        |
34| 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.|
35| Initial value for the decorated variable        | Not allowed.                                    |
36
37Example of a read-only \@ObjectLink decorated variable:
38
39
40```ts
41// The \@ObjectLink decorated variable accepts changes to its attribute.
42this.objLink.a= ...
43// Value assignment is not allowed for the \@ObjectLink decorated variable.
44this.objLink= ...
45```
46
47> **NOTE**
48>
49> Value assignment is not allowed for the \@ObjectLink decorated variable. To assign a value, use [@Prop](arkts-prop.md) instead.
50>
51> - \@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.
52>
53> - \@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.
54
55
56## Variable Transfer/Access Rules
57
58| \@ObjectLink Transfer/Access| Description                                      |
59| ----------------- | ---------------------------------------- |
60| 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).|
61| Synchronize with the source           | Two-way.                                     |
62| Subnode initialization         | Supported; can be used to initialize a regular variable or \@State, \@Link, \@Prop, or \@Provide decorated variable in the child component.|
63
64
65  **Figure 1** Initialization rule
66
67
68![en-us_image_0000001502255262](figures/en-us_image_0000001502255262.png)
69
70
71## Observed Changes and Behavior
72
73
74### Observed Changes
75
76If 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.
77
78
79```ts
80class ClassA {
81  public c: number;
82
83  constructor(c: number) {
84    this.c = c;
85  }
86}
87
88@Observed
89class ClassB {
90  public a: ClassA;
91  public b: number;
92
93  constructor(a: ClassA, b: number) {
94    this.a = a;
95    this.b = b;
96  }
97}
98```
99
100In 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.
101
102
103```ts
104@ObjectLink b: ClassB
105
106// The value assignment can be observed.
107this.b.a = new ClassA(5)
108this.b.b = 5
109
110// ClassA is not decorated by @Observed, and its attribute changes cannot be observed.
111this.b.a.c = 5
112```
113
114\@ObjectLink: \@ObjectLink can only accept instances of classes decorated by \@Observed. The following can be observed:
115
116- Value changes of the attributes that **Object.keys(observedObject)** returns. For details, see [Nested Object](#nested-object).
117
118- 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).
119
120
121### Framework Behavior
122
1231. Initial render:
124   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.
125   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.
126
1272. 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.
128
129
130## Application Scenarios
131
132
133### Nested Object
134
135The following is the data structure of a nested class object.
136
137
138```ts
139// objectLinkNestedObjects.ets
140let NextID: number = 1;
141
142@Observed
143class ClassA {
144  public id: number;
145  public c: number;
146
147  constructor(c: number) {
148    this.id = NextID++;
149    this.c = c;
150  }
151}
152
153@Observed
154class ClassB {
155  public a: ClassA;
156
157  constructor(a: ClassA) {
158    this.a = a;
159  }
160}
161```
162
163
164  The following component hierarchy presents this data structure.
165
166```ts
167@Component
168struct ViewA {
169  label: string = 'ViewA1';
170  @ObjectLink a: ClassA;
171
172  build() {
173    Row() {
174      Button(`ViewA [${this.label}] this.a.c=${this.a.c} +1`)
175        .onClick(() => {
176          this.a.c += 1;
177        })
178    }
179  }
180}
181
182@Entry
183@Component
184struct ViewB {
185  @State b: ClassB = new ClassB(new ClassA(0));
186
187  build() {
188    Column() {
189      ViewA({ label: 'ViewA #1', a: this.b.a })
190      ViewA({ label: 'ViewA #2', a: this.b.a })
191
192      Button(`ViewB: this.b.a.c+= 1`)
193        .onClick(() => {
194          this.b.a.c += 1;
195        })
196      Button(`ViewB: this.b.a = new ClassA(0)`)
197        .onClick(() => {
198          this.b.a = new ClassA(0);
199        })
200      Button(`ViewB: this.b = new ClassB(ClassA(0))`)
201        .onClick(() => {
202          this.b = new ClassB(new ClassA(0));
203        })
204    }
205  }
206}
207```
208
209
210Event handlers in **ViewB**:
211
212
213- this.b.a = new ClassA(0) and this.b = new ClassB(new ClassA(0)): Change to the \@State decorated variable **b** and its attributes.
214
215- this.b.a.c = ... : Second change. [@State](arkts-state.md# observe the change) cannot observe the change of the second layer, but ClassA is decorated by \@Observed, and therefore the change of its attribute c can be observed by \@ObjectLink.
216
217
218Event handlers in **ViewA**:
219
220
221- this.a.c += 1: Changes to the \@ObjectLink decorated variable which 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.
222
223- 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.
224
225
226### Object Array
227
228An object array is a frequently used data structure. The following example shows the usage of array objects.
229
230
231```ts
232@Component
233struct ViewA {
234  // The type of @ObjectLink of the child component ViewA is ClassA.
235  @ObjectLink a: ClassA;
236  label: string = 'ViewA1';
237
238  build() {
239    Row() {
240      Button(`ViewA [${this.label}] this.a.c = ${this.a.c} +1`)
241        .onClick(() => {
242          this.a.c += 1;
243        })
244    }
245  }
246}
247
248@Entry
249@Component
250struct ViewB {
251  // ViewB has the @State decorated ClassA[].
252  @State arrA: ClassA[] = [new ClassA(0), new ClassA(0)];
253
254  build() {
255    Column() {
256      ForEach(this.arrA,
257        (item) => {
258          ViewA({ label: `#${item.id}`, a: item })
259        },
260        (item) => item.id.toString()
261      )
262      // Initialize the @ObjectLink decorated variable using the array item in the @State decorated array, which is an instance of ClassA decorated by @Observed.
263      ViewA({ label: `ViewA this.arrA[first]`, a: this.arrA[0] })
264      ViewA({ label: `ViewA this.arrA[last]`, a: this.arrA[this.arrA.length-1] })
265
266      Button(`ViewB: reset array`)
267        .onClick(() => {
268          this.arrA = [new ClassA(0), new ClassA(0)];
269        })
270      Button(`ViewB: push`)
271        .onClick(() => {
272          this.arrA.push(new ClassA(0))
273        })
274      Button(`ViewB: shift`)
275        .onClick(() => {
276          this.arrA.shift()
277        })
278      Button(`ViewB: chg item property in middle`)
279        .onClick(() => {
280          this.arrA[Math.floor(this.arrA.length / 2)].c = 10;
281        })
282      Button(`ViewB: chg item property in middle`)
283        .onClick(() => {
284          this.arrA[Math.floor(this.arrA.length / 2)] = new ClassA(11);
285        })
286    }
287  }
288}
289```
290
291- this.arrA[Math.floor(this.arrA.length/2)] = new ClassA(..): The change of this state variable triggers two updates.
292  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.
293  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.
294
295- this.arrA.push(new ClassA(0)): The change of this state variable triggers two updates with different effects.
296  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.
297  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.
298
299- this.arrA[Math.floor (this.arrA.length/2)].c: [@State] (arkts-state.md#observe-changes) cannot observe changes in the second layer. However, as **ClassA** is decorated by \@Observed, the change of its attributes will be observed by \@ObjectLink.
300
301
302### Two-Dimensional Array
303
304@Observed class decoration is required for a two-dimensional array. You can declare an \@Observed decorated class that extends from **Array**.
305
306
307```ts
308@Observed
309class StringArray extends Array<String> {
310}
311```
312
313
314
315Declare 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.
316
317
318```ts
319@Observed
320class StringArray extends Array<String> {
321}
322
323@Component
324struct ItemPage {
325  @ObjectLink itemArr: StringArray;
326
327  build() {
328    Row() {
329      Text('ItemPage')
330        .width(100).height(100)
331
332      ForEach(this.itemArr,
333        item => {
334          Text(item)
335            .width(100).height(100)
336        },
337        item => item
338      )
339    }
340  }
341}
342
343@Entry
344@Component
345struct IndexPage {
346  @State arr: Array<StringArray> = [new StringArray(), new StringArray(), new StringArray()];
347
348  build() {
349    Column() {
350      ItemPage({ itemArr: this.arr[0] })
351      ItemPage({ itemArr: this.arr[1] })
352      ItemPage({ itemArr: this.arr[2] })
353
354      Divider()
355
356      ForEach(this.arr,
357        itemArr => {
358          ItemPage({ itemArr: itemArr })
359        },
360        itemArr => itemArr[0]
361      )
362
363      Divider()
364
365      Button('update')
366        .onClick(() => {
367          console.error('Update all items in arr');
368          if (this.arr[0][0] !== undefined) {
369            // We should have a real ID to use with ForEach, but we do no.
370            // Therefore, we need to make sure the pushed strings are unique.
371            this.arr[0].push(`${this.arr[0].slice(-1).pop()}${this.arr[0].slice(-1).pop()}`);
372            this.arr[1].push(`${this.arr[1].slice(-1).pop()}${this.arr[1].slice(-1).pop()}`);
373            this.arr[2].push(`${this.arr[2].slice(-1).pop()}${this.arr[2].slice(-1).pop()}`);
374          } else {
375            this.arr[0].push('Hello');
376            this.arr[1].push('World');
377            this.arr[2].push('!');
378          }
379        })
380    }
381  }
382}
383```
384