You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
33 lines
905 B
33 lines
905 B
2 years ago
|
export default tag Counter < button
|
||
|
# This is the internal state of the tag. We're not exposing it with `prop`
|
||
|
# because we want to read the count from the server:
|
||
|
count = 0
|
||
|
|
||
|
# Mount runs when this tag is first attached to the DOM.
|
||
|
# https://imba.io/docs/components/lifecycle
|
||
|
def mount
|
||
|
# Fetch the count from the server
|
||
|
try
|
||
|
const res = await window.fetch('/count')
|
||
|
const data = await res.json()
|
||
|
count = data.count
|
||
|
catch e
|
||
|
console.error("Couldn't fetch count", e)
|
||
|
|
||
|
def increment
|
||
|
# Increment the counter on the server, then update the client
|
||
|
try
|
||
|
const res = await window.fetch('/increment', {
|
||
|
method: 'POST'
|
||
|
headers: {
|
||
|
'Content-Type': 'application/json'
|
||
|
}
|
||
|
body: JSON.stringify({ increment: 1 })
|
||
|
})
|
||
|
const data = await res.json()
|
||
|
count = data.count
|
||
|
catch e
|
||
|
console.error("Couldn't increment", e)
|
||
|
|
||
|
<self @click=increment> `Count is {count}`
|