You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/reference/lifecycle_callbacks.md
+25Lines changed: 25 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -27,7 +27,9 @@ Method | Invoked by Stimulus…
27
27
------------ | --------------------
28
28
initialize() | Once, when the controller is first instantiated
29
29
connect() | Anytime the controller is connected to the DOM
30
+
[name]TargetConnected(target: Element) | Anytime a target is connected to the DOM
30
31
disconnect() | Anytime the controller is disconnected from the DOM
32
+
[name]TargetDisconnected(target: Element) | Anytime a target is disconnected from the DOM
31
33
32
34
## Connection
33
35
@@ -38,6 +40,15 @@ A controller is _connected_ to the document when both of the following condition
38
40
39
41
When a controller becomes connected, Stimulus calls its `connect()` method.
40
42
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
+
41
52
## Disconnection
42
53
43
54
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
50
61
51
62
When a controller becomes disconnected, Stimulus calls its `disconnect()` method.
52
63
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
+
53
76
## Reconnection
54
77
55
78
A disconnected controller may become connected again at a later time.
56
79
57
80
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.
58
81
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
+
59
84
## Order and Timing
60
85
61
86
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