Hacker News new | ask | show | jobs
by renegadus 2743 days ago
Interesting. Just for fun I created a side-by-side with Kweb of one of their simple examples.

Here is kerax:

  var lines: seq[kstring] = @[]
  
  proc createDom(): VNode =
    result = buildHtml(tdiv):
      button:
        text "Say hello!"
        proc onclick(ev: Event; n: VNode) =
          lines.add "Hello simulated universe"
      for x in lines:
        tdiv:
          text x
  
  setRenderer createDom
Here is the equivalent in Kweb:

  fun main() {
    Kweb(port = 2734) {
        doc.body.new {
            button().text("Say hello!").on.click {
                println("Hello simulated universe")
            }
        }
    }
  }
This compiles and works.
1 comments

The first seems to add new DOM nodes with text on every click. The second one doesn't. So they don't seem to be equivalent.
Hmm, it seems to be adding things to a list but where is it adding a new DOM node?
I would say in

     for x in lines:
        tdiv:
          text x
Whenever the DOM gets rerendered, it iterates over the the lines (or the "model" in general), and creates DOM elements from it. At least it looks very similar to React and Angular code, and they do exactly this.
Ah, you're right. Ok, I think I can do that fairly concisely in Kweb also, if I find a bit of time.