• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# @ohos.app.form.formBindingData (formBindingData)
2
3The **FormBindingData** module provides APIs for widget data binding. You can use the APIs to create a **FormBindingData** object and obtain related information.
4
5> **NOTE**
6>
7> The initial APIs of this module are supported since API version 9. Newly added APIs will be marked with a superscript to indicate their earliest API version.
8
9## Modules to Import
10
11```ts
12import formBindingData from '@ohos.app.form.formBindingData';
13```
14
15
16## ProxyData<sup>10+</sup>
17
18Defines the subscription information about the widget update by proxy.
19
20**Model restriction**: This API can be used only in the stage model.
21
22**System capability**: SystemCapability.Ability.Form
23
24| Name| Type| Mandatory| Description|
25| -------- | -------- | -------- | -------- |
26| key<sup>10+</sup> | string | Yes| Subscriber ID of the widget update by proxy. The value is the same as that of the data publisher.|
27| subscriberId<sup>10+</sup> | string | No| Subscription condition of the widget update by proxy. The default value is the current widget ID (specified by **formId**).|
28
29
30## FormBindingData
31
32Describes a **FormBindingData** object.
33
34**System capability**: SystemCapability.Ability.Form
35
36| Name| Type| Mandatory| Description|
37| -------- | -------- | -------- | -------- |
38| data | Object | Yes| Data to be displayed on the JS widget. The value can be an object containing multiple key-value pairs or a string in JSON format.|
39| proxies<sup>10+</sup> | Array<[proxyData](#proxydata10)> | No| Subscription information of the widget update by proxy. The default value is an empty array.<br>**Model restriction**: This API can be used only in the stage model.<br>|
40
41## createFormBindingData
42
43createFormBindingData(obj?: Object | string): FormBindingData
44
45Creates a **FormBindingData** object.
46
47**System capability**: SystemCapability.Ability.Form
48
49**Parameters**
50
51| Name| Type          | Mandatory| Description                                                        |
52| ------ | -------------- | ---- | ------------------------------------------------------------ |
53| obj    | Object\|string | No  | Data to be displayed on the JS widget. The value can be an object containing multiple key-value pairs or a string in JSON format. The image data is identified by **'formImages'**, and the content is multiple key-value pairs, each of which consists of an image identifier and image file descriptor. The final format is {'formImages': {'key1': fd1, 'key2': fd2}}.|
54
55
56**Return value**
57
58| Type                               | Description                                   |
59| ----------------------------------- | --------------------------------------- |
60| [FormBindingData](#formbindingdata) | **FormBindingData** object created based on the passed data.|
61
62
63**Example**
64
65```ts
66import formBindingData from '@ohos.app.form.formBindingData';
67import fs from '@ohos.file.fs';
68import Base from '@ohos.base';
69
70try {
71  let fd = fs.openSync('/path/to/form.png');
72  let formImagesParam: Record<string, object> = {
73    'image': fd
74  };
75  let createFormBindingDataParam: Record<string, string | Object> = {
76    'name': '21°',
77    'formImages': formImagesParam
78  };
79
80  formBindingData.createFormBindingData(createFormBindingDataParam);
81} catch (error) {
82  let code = (error as Base.BusinessError).code;
83  let message = (error as Base.BusinessError).message;
84  console.error(`catch error, code: ${code}, message: ${message}`);
85}
86```
87