You need to sign in or sign up before continuing.
js-apis-formextension.md 7.3 KB
Newer Older
W
wusongqing 已提交
1 2
# FormExtension

W
wusongqing 已提交
3
> ![icon-note.gif](public_sys-resources/icon-note.gif) **NOTE**<br/>
W
wusongqing 已提交
4
> 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.
W
wusongqing 已提交
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19

Provides **FormExtension** APIs.

## Modules to Import

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

## Required Permissions

None

## Attributes

W
wusongqing 已提交
20 21
**System capability**: SystemCapability.Ability.Form

W
wusongqing 已提交
22
| Name   | Type                                               | Readable| Writable| Description                                               |
W
wusongqing 已提交
23
| ------- | ------------------------------------------------------- | ---- | ---- | --------------------------------------------------- |
W
wusongqing 已提交
24
| context | [FormExtensionContext](js-apis-formextensioncontext.md) | Yes  | No  | Context of the **FormExtension**. This class is inherited from **ExtensionContext**.|
W
wusongqing 已提交
25 26 27 28 29 30 31

## onCreate

onCreate(want: Want): formBindingData.FormBindingData

Called to notify the widget provider that a **Form** instance (widget) has been created.

W
wusongqing 已提交
32
**System capability**: SystemCapability.Ability.Form
W
wusongqing 已提交
33

W
wusongqing 已提交
34 35 36
**Parameters**

  | Name| Type                                  | Mandatory| Description                                                        |
W
wusongqing 已提交
37
  | ------ | -------------------------------------- | ---- | ------------------------------------------------------------ |
W
wusongqing 已提交
38
  | want   | [Want](js-apis-application-Want.md) | Yes  | Information related to the extension, including the widget ID, name, and style. The information must be managed as persistent data to facilitate subsequent widget update and deletion.|
W
wusongqing 已提交
39

W
wusongqing 已提交
40
**Return value**
W
wusongqing 已提交
41

W
wusongqing 已提交
42
  | Type                                                        | Description                                                       |
W
wusongqing 已提交
43 44 45
  | ------------------------------------------------------------ | ----------------------------------------------------------- |
  | [formBindingData.FormBindingData](js-apis-formbindingdata.md#formbindingdata) | A **formBindingData.FormBindingData** object containing the data to be displayed on the widget.|

W
wusongqing 已提交
46
**Example**
W
wusongqing 已提交
47

W
wusongqing 已提交
48
  ```js
W
wusongqing 已提交
49 50 51 52 53 54 55 56 57 58
  export default class MyFormExtension extends FormExtension {
      onCreate(want) {
          console.log('FormExtension onCreate, want:' + want.abilityName);
          let dataObj1 = {
              temperature:"11c",
              "time":"11:00"
          };
          let obj1 = formBindingData.createFormBindingData(dataObj1);
          return obj1;
      }
W
wusongqing 已提交
59 60 61
  }
  ```

W
wusongqing 已提交
62
## FormExtension.onCastToNormal
W
wusongqing 已提交
63 64 65 66 67

onCastToNormal(formId: string): void

Called to notify the widget provider that a temporary widget has been converted to a normal one.

W
wusongqing 已提交
68 69 70
**System capability**: SystemCapability.Ability.Form

**Parameters**
W
wusongqing 已提交
71

W
wusongqing 已提交
72
  | Name| Type  | Mandatory| Description                    |
W
wusongqing 已提交
73
  | ------ | ------ | ---- | ------------------------ |
W
wusongqing 已提交
74
  | formId | string | Yes  | ID of the widget that requests to be converted to a normal one.|
W
wusongqing 已提交
75

W
wusongqing 已提交
76
**Example**
W
wusongqing 已提交
77 78

  ```
W
wusongqing 已提交
79 80 81 82
  export default class MyFormExtension extends FormExtension {
      onCastToNormal(formId) {
          console.log('FormExtension onCastToNormal, formId:' + formId);
      }
W
wusongqing 已提交
83 84 85
  }
  ```

W
wusongqing 已提交
86
## FormExtension.onUpdate
W
wusongqing 已提交
87 88 89 90 91

onUpdate(formId: string): void

Called to notify the widget provider that a widget has been updated. After obtaining the latest data, the caller invokes **updateForm** of the [FormExtensionContext](js-apis-formextensioncontext.md) class to update the widget data.

W
wusongqing 已提交
92
**System capability**: SystemCapability.Ability.Form
W
wusongqing 已提交
93

W
wusongqing 已提交
94 95 96
**Parameters**

  | Name| Type  | Mandatory| Description              |
W
wusongqing 已提交
97
  | ------ | ------ | ---- | ------------------ |
W
wusongqing 已提交
98
  | formId | string | Yes  | ID of the widget that requests to be updated.|
W
wusongqing 已提交
99

W
wusongqing 已提交
100
**Example**
W
wusongqing 已提交
101

W
wusongqing 已提交
102
  ```js
W
wusongqing 已提交
103 104 105 106 107 108 109 110 111 112
  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 已提交
113 114 115
  }
  ```

W
wusongqing 已提交
116
## FormExtension.onVisibilityChange
W
wusongqing 已提交
117 118 119 120 121

onVisibilityChange(newStatus: { [key: string]: number }): void

Called to notify the widget provider of the change of visibility.

W
wusongqing 已提交
122 123 124
**System capability**: SystemCapability.Ability.Form

**Parameters**
W
wusongqing 已提交
125

W
wusongqing 已提交
126
  | Name   | Type                     | Mandatory| Description                        |
W
wusongqing 已提交
127
  | --------- | ------------------------- | ---- | ---------------------------- |
W
wusongqing 已提交
128
  | newStatus | { [key: string]: number } | Yes  | ID and visibility status of the widget to be changed.|
W
wusongqing 已提交
129

W
wusongqing 已提交
130
**Example**
W
wusongqing 已提交
131

W
wusongqing 已提交
132
  ```js
W
wusongqing 已提交
133 134 135 136 137 138 139 140 141 142 143 144 145
  export default class MyFormExtension extends FormExtension {
      onVisibilityChange(newStatus) {
          console.log('FormExtension onVisibilityChange, newStatus:' + newStatus);
          let obj2 = formBindingData.createFormBindingData({temperature:"22c", time:"22:00"});
  
          for (let key in newStatus) {
              console.log('FormExtension onVisibilityChange, key:' + key + ", value=" + newStatus[key]);
              this.context.updateForm(key, obj2)
                  .then((data)=>{
                      console.log('FormExtension context updateForm, data:' + data);
                  }).catch((error) => {
                  console.error('Operation updateForm failed. Cause: ' + error);});
          }
W
wusongqing 已提交
146 147 148 149
      }
  }
  ```

W
wusongqing 已提交
150
## FormExtension.onEvent
W
wusongqing 已提交
151 152 153 154 155

onEvent(formId: string, message: string): void

Called to instruct the widget provider to receive and process the widget event.

W
wusongqing 已提交
156 157 158
**System capability**: SystemCapability.Ability.Form

**Parameters**
W
wusongqing 已提交
159

W
wusongqing 已提交
160
  | Name | Type  | Mandatory| Description                  |
W
wusongqing 已提交
161
  | ------- | ------ | ---- | ---------------------- |
W
wusongqing 已提交
162 163
  | formId  | string | Yes  | ID of the widget that requests the event.|
  | message | string | Yes  | Event message.            |
W
wusongqing 已提交
164

W
wusongqing 已提交
165
**Example**
W
wusongqing 已提交
166

W
wusongqing 已提交
167
  ```js
W
wusongqing 已提交
168 169 170 171
  export default class MyFormExtension extends FormExtension {
      onEvent(formId, message) {
          console.log('FormExtension onEvent, formId:' + formId + ", message:" + message);
      }
W
wusongqing 已提交
172 173 174
  }
  ```

W
wusongqing 已提交
175
## FormExtension.onDestroy
W
wusongqing 已提交
176 177 178 179 180

onDestroy(formId: string): void

Called to notify the widget provider that a **Form** instance (widget) has been destroyed.

W
wusongqing 已提交
181
**System capability**: SystemCapability.Ability.Form
W
wusongqing 已提交
182

W
wusongqing 已提交
183 184 185
**Parameters**

  | Name| Type  | Mandatory| Description              |
W
wusongqing 已提交
186
  | ------ | ------ | ---- | ------------------ |
W
wusongqing 已提交
187
  | formId | string | Yes  | ID of the widget to be destroyed.|
W
wusongqing 已提交
188

W
wusongqing 已提交
189
**Example**
W
wusongqing 已提交
190

W
wusongqing 已提交
191
  ```js
W
wusongqing 已提交
192 193 194 195
  export default class MyFormExtension extends FormExtension {
      onDestroy(formId) {
          console.log('FormExtension onDestroy, formId:' + formId);
      }
W
wusongqing 已提交
196 197
  }
  ```
W
wusongqing 已提交
198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221

## FormExtension.onConfigurationUpdated

onConfigurationUpdated(config: Configuration): void;

Called when the configuration of the environment where the ability is running is updated.

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

**Parameters**

  | Name| Type| Mandatory| Description| 
  | -------- | -------- | -------- | -------- |
  | config | [Configuration](#section188911144124715) | Yes| New configuration.| 

**Example**
    
  ```js
  class MyFormExtension extends MyFormExtension {
      onConfigurationUpdated(config) {
          console.log('onConfigurationUpdated, config:' + JSON.stringify(config));
      }
  }
  ```