js-apis-app-form-formBindingData.md 3.3 KB
Newer Older
1
# @ohos.app.form.formBindingData (formBindingData)
2 3 4 5 6 7 8 9 10 11 12 13 14

The **FormBindingData** module provides APIs for widget data binding. You can use the APIs to create a **FormBindingData** object and obtain related information.

> **NOTE**
>
> 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.

## Modules to Import

```ts
import formBindingData from '@ohos.app.form.formBindingData';
```

15 16 17 18 19 20 21 22 23

## ProxyData<sup>10+</sup>

Defines the subscription information about the widget update by proxy.

**System capability**: SystemCapability.Ability.Form

| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
24 25
| 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.|
| 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**).|
26 27


28 29 30 31 32 33 34 35 36
## FormBindingData

Describes a **FormBindingData** object.

**System capability**: SystemCapability.Ability.Form

| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| 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.|
37
| proxies<sup>10+</sup> | Array<[ProxyData](#proxydata)> | No| Subscription information of the widget update by proxy. The default value is an empty array.|
38 39 40 41 42 43 44 45 46 47 48 49 50

## createFormBindingData

createFormBindingData(obj?: Object | string): FormBindingData

Creates a **FormBindingData** object.

**System capability**: SystemCapability.Ability.Form

**Parameters**

| Name| Type          | Mandatory| Description                                                        |
| ------ | -------------- | ---- | ------------------------------------------------------------ |
51
| 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}}.|
52 53 54 55 56 57 58 59 60 61 62 63


**Return value**

| Type                               | Description                                   |
| ----------------------------------- | --------------------------------------- |
| [FormBindingData](#formbindingdata) | **FormBindingData** object created based on the passed data.|


**Example**

```ts
64
import formBindingData from '@ohos.app.form.formBindingData';
65
import fs from '@ohos.file.fs';
66 67

try {
68
  let fd = fs.openSync('/path/to/form.png');
69
  let obj = {
70 71
    'temperature': '21°',
    'formImages': { 'image': fd }
72
  };
73 74
  formBindingData.createFormBindingData(obj);
} catch (error) {
G
Gloria 已提交
75
  console.error(`catch error, code: ${error.code}, message: ${error.message}`);
76
}
77
```
78 79 80 81 82 83 84 85 86 87 88

## ProxyData

Defines the widget proxy data.

**System capability**: SystemCapability.Ability.Form

| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| key | string | Yes| Key of the proxy. The value depends on the data publisher.|
| subscriberId | string | No| Subscriber ID. The value depends on the data publisher. The default value is the current widget ID.|