Skip to content

Commit 14570ee

Browse files
committed
Add methods initialize, connect and disconnect with code comment
1 parent a6245eb commit 14570ee

File tree

1 file changed

+28
-1
lines changed

1 file changed

+28
-1
lines changed

templates/stimulus/Controller.tpl.php

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,32 @@
1414
<?php endforeach; ?>
1515
}
1616
<?php } ?>
17-
// ...
17+
18+
initialize() {
19+
// Called once when the controller is first instantiated (per element)
20+
21+
// Here you can initialize variables, create scoped callables for event
22+
// listeners, instantiate external libraries, etc.
23+
// this._fooBar = this.fooBar.bind(this)
24+
}
25+
26+
connect() {
27+
// Called every time the controller is connected to the DOM
28+
// (on page load, when it's added to the DOM, moved in the DOM, etc.)
29+
30+
// Here you can add event listeners on the element or target elements,
31+
// add or remove classes, attributes, dispatch custom events, etc.
32+
// this.fooTarget.addEventListener('click', this._fooBar)
33+
}
34+
35+
// Add custom controller actions here
36+
// fooBar() { this.fooTarget.classList.toggle(this.bazClass) }
37+
38+
disconnect() {
39+
// Called anytime its element is disconnected from the DOM
40+
// (on page change, when it's removed from or moved in the DOM, etc.)
41+
42+
// Here you should remove all event listeners added in "connect()"
43+
// this.fooTarget.removeEventListener('click', this._fooBar)
44+
}
1845
}

0 commit comments

Comments
 (0)