Hey,
I was trying to make a simple todo app with karax. The problem is that when adding elements to my notes sequence no redraw is triggered. If i store the notes in localStorage a redraw is triggered, however. Does anybody know whats going on?
include karax / prelude
import json
import dom
type
Note = object
text: string
priority: Priority
Priority = enum
low, mid, high
var notes: seq[Note] = @[]
proc createApp(): VNode =
try:
let x = window.localStorage.getItem("notes")
notes = ($x).parseJson.to(seq[Note])
except:
window.localStorage.setItem("notes", $(%*(notes)))
result = buildHtml(tdiv):
tdiv:
button:
text "new note"
proc onclick(ev: Event; node: VNode) =
notes.add(Note(text: "sample text", priority: high))
#window.localStorage.setItem("notes", $(%*(notes))) #If this line is added the code works as expected
echo "debug"
for n in notes:
tdiv:
p:
text n.text
setRenderer createApp
try:
let x = window.localStorage.getItem("notes")
# notes = ($x).parseJson.to(seq[Note])
notes.add(($x).parseJson.to(seq[Note]))
except:
window.localStorage.setItem("notes", $(%*(notes)))
Thank you. That wasn't quite it, but got me in the right direction.
Karax was actually triggering updates correctly, but as my try/except is inside createApp it would just reset the notes to whatever is in localStorage at the time. So obviously the note added in onclick would be lost if not persisted into localStorage.
With your suggestion whatever is stored in localStorage will be added to the notes sequence on every update.
Moving try/except outside createApp fixes all problems