Skip to content

Commit c182b82

Browse files
committed
ADd documentation to the Lifecycle Callbacks page
1 parent 86720fc commit c182b82

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

docs/reference/lifecycle_callbacks.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,9 @@ Method | Invoked by Stimulus…
2727
------------ | --------------------
2828
initialize() | Once, when the controller is first instantiated
2929
connect() | Anytime the controller is connected to the DOM
30+
[name]TargetConnected(target: Element) | Anytime a target is connected to the DOM
3031
disconnect() | Anytime the controller is disconnected from the DOM
32+
[name]TargetDisconnected(target: Element) | Anytime a target is disconnected from the DOM
3133

3234
## Connection
3335

@@ -38,6 +40,15 @@ A controller is _connected_ to the document when both of the following condition
3840

3941
When a controller becomes connected, Stimulus calls its `connect()` method.
4042

43+
### Targets
44+
45+
A target is _connected_ to the document when both of the following conditions are true:
46+
47+
* its element is present in the document as a descendant of its corresponding controller's element
48+
* its identifier is present in the element's `data-{identifier}-target` attribute
49+
50+
When a target becomes connected, Stimulus calls its controller's `[name]TargetConnected()` method, passing the target element as a parameter.
51+
4152
## Disconnection
4253

4354
A connected controller will later become _disconnected_ when either of the preceding conditions becomes false, such as under any of the following scenarios:
@@ -50,12 +61,26 @@ A connected controller will later become _disconnected_ when either of the prece
5061

5162
When a controller becomes disconnected, Stimulus calls its `disconnect()` method.
5263

64+
### Targets
65+
66+
A connected target will later become _disconnected_ when either of the preceding conditions becomes false, such as under any of the following scenarios:
67+
68+
* the element is explicitly removed from the document with `Node#removeChild()` or `ChildNode#remove()`
69+
* one of the element's parent elements is removed from the document
70+
* one of the element's parent elements has its contents replaced by `Element#innerHTML=`
71+
* the element's `data-{identifier}-target` attribute is removed or modified
72+
* the document installs a new `<body>` element, such as during a Turbo page change
73+
74+
When a target becomes disconnected, Stimulus calls its controller's `[name]TargetDisconnected()` method, passing the target element as a parameter.
75+
5376
## Reconnection
5477

5578
A disconnected controller may become connected again at a later time.
5679

5780
When this happens, such as after removing the controller's element from the document and then re-attaching it, Stimulus will reuse the element's previous controller instance, calling its `connect()` method multiple times.
5881

82+
Similarly, a disconnected target may be connected again at a later time. Stimulus will invoke its controller's `[name]TargetConnected()` method multiple times.
83+
5984
## Order and Timing
6085

6186
Stimulus watches the page for changes asynchronously using the [DOM `MutationObserver` API](https://developer.mozilla.org/en-US/docs/Web/API/MutationObserver).

0 commit comments

Comments
 (0)