singleton.md 2.0 KB
Newer Older
C
ChrisCindy 已提交
1
## Singleton Pattern
D
dolymood 已提交
2

3 4 5 6 7 8 9 10 11
You need to take the singleton and multiton pattern of components into account when invoking the component by API.

- After 1.11

  If the component is singleton, then there will be only one instance when instantiating it multiple times **in same component**, and there is only one corresponding view layer; If the component is multiton, then a new instance will be created each time you instantiate it, and there are multiple corresponding view layers which don't affect each other.

- Below 1.11

  If the component is singleton, then there will be only one instance when instantiating it multiple times, and there is only one corresponding view layer; If the component is multiton, then a new instance will be created each time you instantiate it, and there are multiple corresponding view layers which don't affect each other.
D
dolymood 已提交
12

C
ChrisCindy 已提交
13
The components that are involved with api-invoking in cube-ui are all popups. Frequently used ones among them are listed below:
D
dolymood 已提交
14

C
ChrisCindy 已提交
15 16 17 18 19
- [Toast](#/en-US/docs/toast)
- [Picker](#/en-US/docs/picker)
- [TimePicker](#/en-US/docs/time-picker)
- [Dialog](#/en-US/docs/dialog)
- [ActionSheet](#/en-US/docs/action-sheet)
D
dolymood 已提交
20

C
ChrisCindy 已提交
21
All the api-invokings are implemented with the `createAPI` function exported by the [create-api](#/en-US/docs/create-api) module. It is decided whether the component is singleton in definition. See details in [create-api](#/en-US/docs/create-api).
D
dolymood 已提交
22

C
ChrisCindy 已提交
23
By default, Toast, Dialog and ActionSheet is singleton, while Picker and TimePicker is multiton because their scenes tend to be more complex with a lot of extra data processing. If you want to change the default behavior at the time of instantiation,you can modify the parameter of `$createX`. For example, we change the Dialog into multiton:
D
dolymood 已提交
24 25 26 27 28 29 30 31 32 33

```js
const dialog = this.$createDialog({
  type: 'confirm',
  title: 'title',
  content: 'content'
}, false)
dialog.show()
```

C
ChrisCindy 已提交
34
In general cases, default behavior can meet the demand, except you have special need.
D
dolymood 已提交
35

C
ChrisCindy 已提交
36
**Note:** Considering the scenes of Picker and TimePicker, neither of them support singleton pattern.