Skip to content

Commit e09a45a

Browse files
committed
Update tests with base methods
1 parent f6adf79 commit e09a45a

File tree

2 files changed

+56
-2
lines changed

2 files changed

+56
-2
lines changed

tests/fixtures/make-stimulus-controller/with_targets.js

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,32 @@ import { Controller } from '@hotwired/stimulus';
88
/* stimulusFetch: 'lazy' */
99
export default class extends Controller {
1010
static targets = ['results', 'messages', 'errors']
11-
// ...
11+
12+
initialize() {
13+
// Called once when the controller is first instantiated (per element)
14+
15+
// Here you can initialize variables, create scoped callables for event
16+
// listeners, instantiate external libraries, etc.
17+
// this._fooBar = this.fooBar.bind(this)
18+
}
19+
20+
connect() {
21+
// Called every time the controller is connected to the DOM
22+
// (on page load, when it's added to the DOM, moved in the DOM, etc.)
23+
24+
// Here you can add event listeners on the element or target elements,
25+
// add or remove classes, attributes, dispatch custom events, etc.
26+
// this.fooTarget.addEventListener('click', this._fooBar)
27+
}
28+
29+
// Add custom controller actions here
30+
// fooBar() { this.fooTarget.classList.toggle(this.bazClass) }
31+
32+
disconnect() {
33+
// Called anytime its element is disconnected from the DOM
34+
// (on page change, when it's removed from or moved in the DOM, etc.)
35+
36+
// Here you should remove all event listeners added in "connect()"
37+
// this.fooTarget.removeEventListener('click', this._fooBar)
38+
}
1239
}

tests/fixtures/make-stimulus-controller/without_targets.js

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,32 @@ import { Controller } from '@hotwired/stimulus';
77

88
/* stimulusFetch: 'lazy' */
99
export default class extends Controller {
10-
// ...
10+
11+
initialize() {
12+
// Called once when the controller is first instantiated (per element)
13+
14+
// Here you can initialize variables, create scoped callables for event
15+
// listeners, instantiate external libraries, etc.
16+
// this._fooBar = this.fooBar.bind(this)
17+
}
18+
19+
connect() {
20+
// Called every time the controller is connected to the DOM
21+
// (on page load, when it's added to the DOM, moved in the DOM, etc.)
22+
23+
// Here you can add event listeners on the element or target elements,
24+
// add or remove classes, attributes, dispatch custom events, etc.
25+
// this.fooTarget.addEventListener('click', this._fooBar)
26+
}
27+
28+
// Add custom controller actions here
29+
// fooBar() { this.fooTarget.classList.toggle(this.bazClass) }
30+
31+
disconnect() {
32+
// Called anytime its element is disconnected from the DOM
33+
// (on page change, when it's removed from or moved in the DOM, etc.)
34+
35+
// Here you should remove all event listeners added in "connect()"
36+
// this.fooTarget.removeEventListener('click', this._fooBar)
37+
}
1138
}

0 commit comments

Comments
 (0)