forked from SymfonyCasts/symfony-ux
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtargets-add-target.diff
36 lines (32 loc) · 1.18 KB
/
targets-add-target.diff
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
diff --git a/assets/controllers/counter_controller.js b/assets/controllers/counter_controller.js
index 320d85e..1a95704 100644
--- a/assets/controllers/counter_controller.js
+++ b/assets/controllers/counter_controller.js
@@ -1,15 +1,14 @@
import { Controller } from 'stimulus';
export default class extends Controller {
+ static targets = ['count'];
+
connect() {
this.count = 0;
- const counterNumberElement = this.element
- .getElementsByClassName('counter-count')[0];
-
this.element.addEventListener('click', () => {
this.count++;
- counterNumberElement.innerText = this.count;
+ this.countTarget.innerText = this.count;
});
}
}
diff --git a/templates/product/index.html.twig b/templates/product/index.html.twig
index afaa872..eb7d337 100644
--- a/templates/product/index.html.twig
+++ b/templates/product/index.html.twig
@@ -11,7 +11,7 @@
<div data-controller="counter">
I have been clicked
- <span class="counter-count">0</span>
+ <span data-counter-target="count">0</span>
times!
</div>