js-apis-formextensioncontext.md 3.6 KB
Newer Older
W
wusongqing 已提交
1 2
# FormExtensionContext

W
wusongqing 已提交
3 4 5 6
> **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. 
> The APIs of this module can be used only in the stage model.
W
wusongqing 已提交
7 8 9

Implements the context that provides the capabilities and APIs of **FormExtension**. This class is inherited from **ExtensionContext**.

W
wusongqing 已提交
10 11 12 13 14 15
## Modules to Import

```js
import FormExtension from '@ohos.application.FormExtension';
```

W
wusongqing 已提交
16
## FormExtensionContext.updateForm
W
wusongqing 已提交
17 18 19 20 21

updateForm(formId: string, formBindingData: formBindingData.FormBindingData, callback: AsyncCallback\<void>): void

Updates a widget. This method uses a callback to return the result.

W
wusongqing 已提交
22
**System capability**: SystemCapability.Ability.Form
W
wusongqing 已提交
23

W
wusongqing 已提交
24 25
**Parameters**

W
wusongqing 已提交
26 27 28 29 30
  | Name         | Type                                                        | Mandatory| Description                                  |
  | --------------- | ------------------------------------------------------------ | ---- | -------------------------------------- |
  | formId          | string                                                       | Yes  | ID of the widget that requests to be updated.                    |
  | formBindingData | [formBindingData.FormBindingData](js-apis-formbindingdata.md#formbindingdata) | Yes  | New data of the widget.                        |
  | callback        | AsyncCallback\<void>                                         | Yes  | Callback used to return the result indicating whether the method is successfully called.|
W
wusongqing 已提交
31

W
wusongqing 已提交
32
**Example**
W
wusongqing 已提交
33

W
wusongqing 已提交
34
  ```js
W
wusongqing 已提交
35 36 37 38 39 40 41 42 43 44 45 46
  import formBindingData from '@ohos.application.formBindingData'
  export default class MyFormExtension extends FormExtension {
      onUpdate(formId) {
          console.log('FormExtension onUpdate, formId:' + formId);
          let obj2 = formBindingData.createFormBindingData({temperature:"22c", time:"22:00"});
          this.context.updateForm(formId, obj2, (data)=>{
              console.log('FormExtension context updateForm, data:' + data);
          });
      }
  }


W
wusongqing 已提交
47 48
  ```

W
wusongqing 已提交
49
## FormExtensionContext.updateForm
W
wusongqing 已提交
50 51 52 53 54

updateForm(formId: string, formBindingData: formBindingData.FormBindingData): Promise\<void>

Updates a widget. This method uses a promise to return the result.

W
wusongqing 已提交
55 56 57
**System capability**: SystemCapability.Ability.Form

**Parameters**
W
wusongqing 已提交
58

W
wusongqing 已提交
59 60 61 62
  | Name         | Type                                                        | Mandatory| Description              |
  | --------------- | ------------------------------------------------------------ | ---- | ------------------ |
  | formId          | string                                                       | Yes  | ID of the widget that requests to be updated.|
  | formBindingData | [formBindingData.FormBindingData](js-apis-formbindingdata.md#formbindingdata) | Yes  | New data of the widget.    |
W
wusongqing 已提交
63

W
wusongqing 已提交
64
**Return value**
W
wusongqing 已提交
65

W
wusongqing 已提交
66 67 68
  | Type          | Description                             |
  | -------------- | --------------------------------- |
  | Promise\<void> | Promise returned with the result indicating whether the method is successfully called.|
W
wusongqing 已提交
69

W
wusongqing 已提交
70
**Example**
W
wusongqing 已提交
71

W
wusongqing 已提交
72 73 74 75 76 77 78 79 80 81 82 83 84 85
  ```js
  import formBindingData from '@ohos.application.formBindingData'
  export default class MyFormExtension extends FormExtension {
      onUpdate(formId) {
          console.log('FormExtension onUpdate, formId:' + formId);
          let obj2 = formBindingData.createFormBindingData({temperature:"22c", time:"22:00"});
          this.context.updateForm(formId, obj2)
              .then((data)=>{
                  console.log('FormExtension context updateForm, data:' + data);
              }).catch((error) => {
              console.error('Operation updateForm failed. Cause: ' + error);});
      }
  }

W
wusongqing 已提交
86
  ```