Skip to content

Commit 2356bb2

Browse files
Jeslyn BoJeslyn Bo
authored andcommitted
DOC-2898: Clarify usage of onSetup in documentation with improved descriptions for component creation and destruction callbacks.
1 parent 99befda commit 2356bb2

1 file changed

Lines changed: 8 additions & 6 deletions

File tree

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,27 @@
11
[[using-onsetup]]
22
== Using `+onSetup+`
33

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 components API. This function should return a callback that returns nothing after being passed the components 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.
55

66
To clarify, in code `+onSetup+` may look like this:
77

88
[source,js]
99
----
1010
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
1213
1314
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
1517
};
1618
};
1719
----
1820

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)+`.
2022

2123
[NOTE]
2224
====
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 () => {};+`.
2527
====

0 commit comments

Comments
 (0)