General improvements and fixes (#58)

* Fixes Nimble package structure.
* Improves readme.
* Fixes compatibility with devel.
* Add nice error message when ROOT is missing.
* Pass responseText instead of statusText in ajax callbacks.
* Implements verbatim node.
* Apparently it should just be true all the time.
* Small fix to readme.
* Fixes verbatim node updates.
* Move karun into the `karax` directory so that it's installable.
This commit is contained in:
Dominik Picheta 2018-05-23 22:30:06 +01:00 • committed by Andreas Rumpf
commit 9068686614
6 changed files with 31 additions and 6 deletions

View file

@ -9,6 +9,6 @@ license = "MIT"
requires "nim >= 0.16.1"
skipDirs = @["examples", "experiments"]
skipDirs = @["examples", "experiments", "tests"]
bin = @["tools/karun"]
bin = @["karax/tools/karun"]

View file

@ -38,7 +38,7 @@ proc ajax*(meth, url: cstring; headers: openarray[(cstring, cstring)];
if this.status == 200:
contWrapper(this.status, this.responseText)
else:
contWrapper(this.status, this.statusText)
contWrapper(this.status, this.responseText)
if useBinary:
ajax.send(blob)
else:

View file

@ -4,6 +4,8 @@ import kdom, vdom, jstrutils, compact, jdict, vstyles
export kdom.Event, kdom.Blob
{.experimental: "notnil".}
proc kout*[T](x: T) {.importc: "console.log", varargs, deprecated.}
## the preferred way of debugging karax applications. Now deprecated,
## you can now use ``system.echo`` instead.
@ -135,6 +137,10 @@ proc vnodeToDom*(n: VNode; kxi: KaraxInstance): Node =
if n.kind == VNodeKind.text:
result = document.createTextNode(n.text)
attach n
elif n.kind == VNodeKind.verbatim:
result = document.createElement("div")
result.innerHTML = n.text
return result
elif n.kind == VNodeKind.vthunk:
let x = callThunk(vcomponents[n.text], n)
result = vnodeToDom(x, kxi)
@ -183,6 +189,8 @@ proc same(n: VNode, e: Node; nesting = 0): bool =
if kxi.orphans.contains(n.id): return true
if n.kind == VNodeKind.component:
result = same(VComponent(n).expanded, e, nesting+1)
elif n.kind == VNodeKind.verbatim:
result = true
elif n.kind == VNodeKind.vthunk or n.kind == VNodeKind.dthunk:
# we don't check these for now:
result = true
@ -239,6 +247,9 @@ proc eq(a, b: VNode): EqResult =
if a.len != b.len: return different
for i in 0..<a.len:
if eq(a[i], b[i]) == different: return different
elif a.kind == VNodeKind.verbatim:
if a.text != b.text:
return different
elif b.kind == VNodeKind.component:
# different component names mean different components:
if a.text != b.text:
@ -632,6 +643,11 @@ proc setRenderer*(renderer: proc (data: RouterData): VNode,
proc (data: RouterData) = nil): KaraxInstance {.
discardable.} =
## Setup Karax. Usually the return value can be ignored.
if document.getElementById(root).isNil:
let msg = "Could not find a <div> with id=" & root &
". Karax needs it as its rendering target."
raise newException(Exception, $msg)
result = KaraxInstance(rootId: root, renderer: renderer,
postRenderCallback: clientPostRenderCallback,
patches: newSeq[Patch](60),

View file

@ -14,7 +14,7 @@ type
VNodeKind* {.pure.} = enum
text = "#text", int = "#int", bool = "#bool",
vthunk = "#vthunk", dthunk = "#dthunk",
component = "#component",
component = "#component", verbatim="#verbatim",
html, head, title, base, link, meta, style,
script, noscript,
@ -267,6 +267,13 @@ when defined(js):
proc text*(s: string): VNode = VNode(kind: VNodeKind.text, text: kstring(s), index: -1)
proc text*(s: kstring): VNode = VNode(kind: VNodeKind.text, text: s, index: -1)
when defined(js):
proc verbatim*(s: string): VNode =
VNode(kind: VNodeKind.verbatim, text: kstring(s), index: -1)
proc verbatim*(s: kstring): VNode =
VNode(kind: VNodeKind.verbatim, text: s, index: -1)
iterator items*(n: VNode): VNode =
for i in 0..<n.kids.len: yield n.kids[i]

View file

@ -7,9 +7,11 @@ to change.
To try it out, run::
git clone https://github.com/pragmagic/karax
cd ~/projects # Insert your favourite directory for projects
nimble develop karax # This will clone Karax and create a link to it in ~/.nimble
cd karax
nimble install # For first time
cd examples/todoapp
nim js todoapp.nim