Created
February 28, 2020 11:47
-
-
Save caius/457ae7b2d490375c67c5e066e6142dfb to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!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