karaxnim / karax

Karax. Single page applications for Nim.
MIT License
1.08k stars 91 forks source link

Should redraw trigger here? #92

Closed kidandcat closed 5 years ago

kidandcat commented 5 years ago

After fetching the list of channels and saving them in channels var, the DOM is not refreshed (only after pressing the close button, the list redraws) so for it to work I'm calling redraw.

Do I need to call redraw in situations like this one? or should it works without redraw()?

import karax / [kbase, vdom, kdom, vstyles, karax, karaxdsl, jjson], jsffi, asyncjs, jsutils, strutils

var channels: seq[tuple[name: kstring, url: kstring]] = @[]
var hide = false

proc parseM3u(text: string): seq[tuple[name: kstring, url: kstring]] =
 ...

proc fetchChannels(): void {.async.} =
  log("fetching channels".kstring)
  var res = await fetchText("https://admin.galax.be/TVkanalen/external.m3u")
  channels = parseM3u($res)  
  log("got ".kstring & $channels.len() & " channels".kstring)
  redraw()

proc createDom(): VNode =
  result = buildHtml(tdiv):
    tdiv(class="channel-container"):
      tdiv(class="stop"):
        proc onclick() =
          stop()
        text "Stop"
      for channel in channels:
        tdiv(class="channel", id=channel.url):
          proc onclick() =
            changeSource(channel.url)
          text channel.name

fetchChannels()
setRenderer createDom
Araq commented 5 years ago

Works as indented, Karax redraws on event callbacks and inside kajax.nim if requested. That keeps things easy to understand.