You need to sign in or sign up before continuing.
js-apis-formextension.md 5.2 KB
Newer Older
W
wusongqing 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171
# FormExtension

> ![icon-note.gif](public_sys-resources/icon-note.gif) **Note:**
> The initial APIs of this module are supported since API version 8. Newly added APIs will be marked with a superscript to indicate their earliest API version.

Provides **FormExtension** APIs.

## Modules to Import

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

## Required Permissions

None

## Attributes

| Name| Type| Readable| Writable| Description|
| ------- | ------------------------------------------------------- | ---- | ---- | --------------------------------------------------- |
| context | [FormExtensionContext](js-apis-formextensioncontext.md) | Yes| No| Context of the **FormExtension**. This class is inherited from **ExtensionContext**.|

## onCreate

onCreate(want: Want): formBindingData.FormBindingData

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

- Parameters

  | Name| Type| Mandatory| Description|
  | ------ | -------------------------------------- | ---- | ------------------------------------------------------------ |
  | want   | [Want](js-apis-featureAbility.md#want) | 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.|

- Return value

  | Type| Description|
  | ------------------------------------------------------------ | ----------------------------------------------------------- |
  | [formBindingData.FormBindingData](js-apis-formbindingdata.md#formbindingdata) | A **formBindingData.FormBindingData** object containing the data to be displayed on the widget.|

- Example

  ```
  onCreate(want) {
      console.log('FormExtension onCreate, want:' + want.abilityName);
      let dataObj1 = {
          temperature:"11c",
          "time":"11:00"
      };
      let obj1 = formBindingData.createFormBindingData(dataObj1);
      return obj1;
  }
  ```

## onCastToNormal

onCastToNormal(formId: string): void

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

- Parameters

  | Name| Type| Mandatory| Description|
  | ------ | ------ | ---- | ------------------------ |
  | formId | string | Yes| ID of the widget that requests to be converted to a normal one.|

- Example

  ```
  onCastToNormal(formId) {
      console.log('FormExtension onCastToNormal, formId:' + formId);
  }
  ```

## onUpdate

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.

- Parameters

  | Name| Type| Mandatory| Description|
  | ------ | ------ | ---- | ------------------ |
  | formId | string | Yes| ID of the widget that requests to be updated.|

- Example

  ```
  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);});
  }
  ```

## onVisibilityChange

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

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

- Parameters

  | Name| Type| Mandatory| Description|
  | --------- | ------------------------- | ---- | ---------------------------- |
  | newStatus | { [key: string]: number } | Yes| ID and visibility status of the widget to be changed.|

- Example

  ```
  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);});
      }
  }
  ```

## onEvent

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

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

- Parameters

  | Name| Type| Mandatory| Description|
  | ------- | ------ | ---- | ---------------------- |
  | formId  | string | Yes| ID of the widget that requests the event.|
  | message | string | Yes| Event message.|

- Example

  ```
  onEvent(formId, message) {
      console.log('FormExtension onEvent, formId:' + formId + ", message:" + message);
  }
  ```

## onDestroy

onDestroy(formId: string): void

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

- Parameters

  | Name| Type| Mandatory| Description|
  | ------ | ------ | ---- | ------------------ |
  | formId | string | Yes| ID of the widget to be destroyed.|

- Example

  ```
  onDestroy(formId) {
      console.log('FormExtension onDestroy, formId:' + formId);
  }
  ```