Add count controller
This commit is contained in:
parent
d379fdb73d
commit
7146a7f66d
35
assets/js/controllers/count_controller.js
Normal file
35
assets/js/controllers/count_controller.js
Normal file
|
@ -0,0 +1,35 @@
|
|||
import { Controller } from "@hotwired/stimulus";
|
||||
|
||||
export default class extends Controller {
|
||||
static targets = ["result"];
|
||||
|
||||
static values = {
|
||||
count: Number,
|
||||
};
|
||||
|
||||
connect() {
|
||||
console.log("count#connect");
|
||||
|
||||
this.resultTarget.textContent = this.countValue;
|
||||
}
|
||||
|
||||
countValueChanged() {
|
||||
console.log("count#countValueChanged");
|
||||
|
||||
this.resultTarget.textContent = this.countValue;
|
||||
}
|
||||
|
||||
decrement() {
|
||||
console.log("count#decrement");
|
||||
|
||||
if (this.countValue > 0) {
|
||||
this.countValue = this.countValue - 1;
|
||||
}
|
||||
}
|
||||
|
||||
increment() {
|
||||
console.log("count#increment");
|
||||
|
||||
this.countValue = this.countValue + 1;
|
||||
}
|
||||
}
|
|
@ -4,8 +4,13 @@
|
|||
<title>Stimulus esbuild example</title>
|
||||
</head>
|
||||
<body>
|
||||
<div data-controller="hello">
|
||||
<h1>Hello, World!</h1>
|
||||
<div data-controller="count">
|
||||
<p>Count: <span data-count-target="result"></span></p>
|
||||
|
||||
<div>
|
||||
<button type="button" data-action="count#increment">+</button>
|
||||
<button type="button" data-action="count#decrement">-</button>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<script src="/build/app.js"></script>
|
||||
|
|
Loading…
Reference in a new issue