Skip to content
This repository was archived by the owner on Oct 26, 2024. It is now read-only.

Commit ace7261

Browse files
Merge pull request #4 from stimulus-components/feature/using-target-only
Using target to bind event listener
2 parents 0b09c1e + e5f6298 commit ace7261

File tree

4 files changed

+18
-9
lines changed

4 files changed

+18
-9
lines changed

CHANGELOG.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,15 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
66

77
## [Unreleased]
88

9+
### Changed
10+
11+
- **Breaking** `data-target="checkbox-select-all.checkboxAll"` is now required.
12+
- **Breaking** `data-action="change->checkbox-select-all#toggle"` has been be removed.
13+
```diff
14+
- <input type="checkbox" data-action="change->checkbox-select-all#toggle" />
15+
+ <input type="checkbox" data-target="checkbox-select-all.checkboxAll" />
16+
```
17+
918
## [1.1.0] - 2020-10-29
1019

1120
### Added

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ application.register("checkbox-select-all", CheckboxSelectAll)
3535
<tbody>
3636
<td class="block">
3737
<label>
38-
<input type="checkbox" data-target="checkbox-select-all.checkboxAll" data-action="change->checkbox-select-all#toggle" />
38+
<input type="checkbox" data-target="checkbox-select-all.checkboxAll" />
3939
<span>Select All / Deselect All</span>
4040
</label>
4141
</td>
@@ -104,7 +104,7 @@ In your view:
104104
```html
105105
<%= form_with model: @user, data: { controller: 'checkbox-select-all' } do |f| %>
106106
<label>
107-
<input type="checkbox" data-target="checkbox-select-all.checkboxAll" data-action="change->checkbox-select-all#toggle" />
107+
<input type="checkbox" data-target="checkbox-select-all.checkboxAll" />
108108
<span>Select All / Deselect All</span>
109109
</label>
110110

index.html

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -98,12 +98,7 @@ <h2 class="text-center text-2xl font-bold my-4">Essential links</h2>
9898
<tbody>
9999
<td class="block">
100100
<label>
101-
<input
102-
id="checkbox-select-all"
103-
type="checkbox"
104-
data-target="checkbox-select-all.checkboxAll"
105-
data-action="change->checkbox-select-all#toggle"
106-
/>
101+
<input id="checkbox-select-all" type="checkbox" data-target="checkbox-select-all.checkboxAll" />
107102
<span>Select All / Deselect All</span>
108103
</label>
109104
</td>

src/index.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,23 @@ import { Controller } from 'stimulus'
33
export default class extends Controller {
44
static targets = ['checkboxAll', 'checkbox']
55

6-
connect () {
6+
initialize () {
7+
this.toggle = this.toggle.bind(this)
78
this.refresh = this.refresh.bind(this)
9+
}
810

11+
connect () {
912
if (!this.hasCheckboxAllTarget) return
1013

14+
this.checkboxAllTarget.addEventListener('change', this.toggle)
1115
this.checkboxTargets.forEach(checkbox => checkbox.addEventListener('change', this.refresh))
1216
this.refresh()
1317
}
1418

1519
disconnect () {
1620
if (!this.hasCheckboxAllTarget) return
1721

22+
this.checkboxAllTarget.removeEventListener('change', this.toggle)
1823
this.checkboxTargets.forEach(checkbox => checkbox.removeEventListener('change', this.refresh))
1924
}
2025

0 commit comments

Comments
 (0)