2021-10-17 18:26:00 +02:00
|
|
|
---
|
2022-10-12 21:52:47 +02:00
|
|
|
title: Lifecycle hook methods
|
|
|
|
order: 110
|
2021-10-17 18:26:00 +02:00
|
|
|
---
|
2021-08-25 16:09:31 +02:00
|
|
|
|
2022-10-12 21:52:47 +02:00
|
|
|
FreeSewing plugins can provide hooks, which is a way to hook into the pattern's
|
|
|
|
lifecycle.
|
2021-08-25 16:09:31 +02:00
|
|
|
|
2022-10-12 21:52:47 +02:00
|
|
|
## Signature
|
2021-08-25 16:09:31 +02:00
|
|
|
|
2022-10-12 21:52:47 +02:00
|
|
|
To provide one or more hooks, your plugin should have a `hooks` property that
|
2022-12-25 21:13:48 -08:00
|
|
|
is an object where the keys are the lifecycle hook name and the value holds a
|
2022-10-12 21:52:47 +02:00
|
|
|
method. When the lifecycle hook is triggered, your method will be called.
|
2021-08-25 16:09:31 +02:00
|
|
|
|
2022-10-12 21:52:47 +02:00
|
|
|
```mjs
|
|
|
|
const myPlugin = {
|
|
|
|
name: 'example',
|
|
|
|
version: '0.0.1',
|
|
|
|
hooks: {
|
|
|
|
hookName: function (obj, data = {}) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
|
|
|
If you want to attach multiple methods to the same lifecycle hook, you can pass
|
|
|
|
them as an array:
|
|
|
|
|
|
|
|
```mjs
|
|
|
|
const myPlugin = {
|
|
|
|
name: 'example',
|
|
|
|
version: '0.0.1',
|
|
|
|
hooks: {
|
|
|
|
hookName: [
|
|
|
|
function one (obj, data = {}) { },
|
|
|
|
function two (obj, data = {}) { }
|
|
|
|
]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
|
|
|
## Arguments
|
|
|
|
|
|
|
|
All lifecycle methods will receive two parameters:
|
|
|
|
|
|
|
|
- An object relevant to the lifecycle hook. See the [hooks API reference](/reference/hooks/) for details.
|
2022-02-20 14:44:38 +01:00
|
|
|
- Data passed when the hook was registered (optional)
|
2022-10-12 21:52:47 +02:00
|
|
|
|
|
|
|
## Notes
|
|
|
|
|
|
|
|
Refer to the [hooks API reference](/reference/hooks/) for a list of all
|
|
|
|
available lifecycle hooks.
|
|
|
|
|