Skip to content

Instantly share code, notes, and snippets.

@caius
Created February 28, 2020 11:47
Show Gist options
  • Save caius/457ae7b2d490375c67c5e066e6142dfb to your computer and use it in GitHub Desktop.
Save caius/457ae7b2d490375c67c5e066e6142dfb to your computer and use it in GitHub Desktop.
<!DOCTYPE html>
<html lang="en">
<head>
<script src="https://unpkg.com/stimulus/dist/stimulus.umd.js"></script>
<script>
(() => {
const counterController = class extends Stimulus.Controller {
static get targets() {
return [ "count" ]
}
connect() {
this.setCount(0)
this.countTarget.text = "1"
}
increment() {
this.setCount(this.getCount() + 1)
}
decrement() {
this.setCount(this.getCount() - 1)
}
getCount() {
return this.counter
}
setCount(i) {
this.counter = i
this.countTarget.textContent = i
}
}
const application = Stimulus.Application.start()
application.register("counter", counterController)
})()
</script>
</head>
<body>
<div data-controller="counter">
<p>
Counter: <span data-target="counter.count"></span>
</p>
<p>
<button data-action="click->counter#increment">++</button>
<button data-action="click->counter#decrement">--</button>
</p>
</div>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment