|
1 | 1 | [[using-onsetup]] |
2 | 2 | == Using `+onSetup+` |
3 | 3 |
|
4 | | -`+onSetup+` is a complex property. It takes a function that is passed the component's API and should return a callback that is passed the component's API and returns nothing. This occurs because `+onSetup+` runs whenever the component is rendered, and the returned callback is executed when the component is destroyed. This is essentially an `+onTeardown+` handler, and can be used to unbind events and callbacks. |
| 4 | +`+onSetup+` accepts a function that receives the component’s API. This function should return a callback that returns nothing after being passed the component’s API. This occurs because `+onSetup+` runs whenever the component is rendered, and the callback returned by `+onSetup+` is executed when the component is destroyed. The function returned from `+onSetup+` is essentially an `+onTeardown+` handler, and can be used to unbind events and callbacks. |
5 | 5 |
|
6 | 6 | To clarify, in code `+onSetup+` may look like this: |
7 | 7 |
|
8 | 8 | [source,js] |
9 | 9 | ---- |
10 | 10 | onSetup: (api) => { |
11 | | - // Do something here on component render, like set component properties or bind an event listener |
| 11 | + // Runs when the component is created |
| 12 | + // Configure the component or bind event listeners |
12 | 13 |
|
13 | 14 | return (api) => { |
14 | | - // Do something here on teardown, like unbind an event listener |
| 15 | + // Runs when the component is destroyed |
| 16 | + // Unbind event listeners or clean up resources |
15 | 17 | }; |
16 | 18 | }; |
17 | 19 | ---- |
18 | 20 |
|
19 | | -To bind a callback function to an editor event use `+editor.on(eventName, callback)+`. To unbind an event listener use `+editor.off(eventName, callback)+`. Any event listeners _should_ be unbound in the teardown callback. The only editor event which does not need to be unbound is `+init+` e.g. `+editor.on('init', callback)+`. |
| 21 | +To bind a callback function to an editor event use `xref:apis/tinymce.editor.adoc#on[`+editor.off(eventName, callback)+`]. To unbind an event listener use `xref:apis/tinymce.editor.adoc#off[`+editor.off(eventName, callback)+`]. Any event listeners _should_ be unbound in the teardown callback. The only editor event which does not need to be unbound is `+init+` e.g. `+editor.on('init', callback)+`. |
20 | 22 |
|
21 | 23 | [NOTE] |
22 | 24 | ==== |
23 | | -* The callback function for `+editor.off()+` should be the same function passed to `+editor.on()+`. For example, if a `+editorEventCallback+` function is bound to the `+NodeChange+` event when the button is created, `+onSetup+` should return `+(api) => editor.off('NodeChange', editorEventCallback)+`. |
24 | | -* If `+onSetup+` does not have any event listeners or only listens to the `+init+` event, `+onSetup+` can return an empty function e.g. `+return () => {};+`. |
| 25 | +* The callback function passed to `+editor.off()+` should be the same function passed to `+editor.on()+`. For example, if an `+editorEventCallback+` function is bound to the `+NodeChange+` event when the button is created, `+onSetup+` should return `+(api) => editor.off('NodeChange', editorEventCallback)+`. |
| 26 | +* If `+onSetup+` does not register any event listeners or only listens to the `+init+` event, `+onSetup+` can return an empty function e.g. `+return () => {};+`. |
25 | 27 | ==== |
0 commit comments