Skip to content

Commit b415ce5

Browse files
Add query string and fix hash (#1) (#258)
* Update karax.nim Add some fixes * Update jstrutils.nim * Update karax.nim
1 parent ccb0503 commit b415ce5

File tree

2 files changed

+14
-2
lines changed

2 files changed

+14
-2
lines changed

karax/jstrutils.nim

+2
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,5 @@ proc isInt*(s: cstring): bool {.asmNoStackFrame.} =
3131

3232
proc parseInt*(s: cstring): int {.importcpp: "parseInt(#, 10)", nodecl.}
3333
proc parseFloat*(s: cstring): BiggestFloat {.importc, nodecl.}
34+
35+
proc join*(a: openArray[cstring], sep = cstring""): cstring {.importcpp: "(#.join(#))", nodecl.}

karax/karax.nim

+12-2
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ type
2727
type
2828
RouterData* = ref object ## information that is passed to the 'renderer' callback
2929
hashPart*: cstring ## the hash part of the URL for routing.
30+
queryString*: cstring ## The search string, can be used for passing data to karax
3031

3132
KaraxInstance* = ref object ## underlying karax instance. Usually you don't have
3233
## know about this.
@@ -637,6 +638,7 @@ proc runDiff*(kxi: KaraxInstance; oldNode, newNode: VNode) =
637638

638639
var onhashChange {.importc: "window.onhashchange".}: proc()
639640
var hashPart {.importc: "window.location.hash".}: cstring
641+
var queryString {.importc: "window.location.search".}: cstring
640642

641643
proc avoidDomDiffing*(kxi: KaraxInstance = kxi) =
642644
## enforce a full redraw for the next redraw operation.
@@ -660,8 +662,16 @@ proc dodraw(kxi: KaraxInstance) =
660662
return
661663

662664
kxi.rendering = true
663-
664-
let rdata = RouterData(hashPart: hashPart)
665+
666+
var rdata = RouterData()
667+
if cstring"?" in hashPart:
668+
let hashSplit = hashPart.split(cstring"?")
669+
rdata.hashPart = hashSplit[0]
670+
rdata.queryString = join(hashSplit[1..^1], cstring"?")
671+
else:
672+
rdata.hashPart = hashPart
673+
rdata.queryString = queryString
674+
665675
let newtree = kxi.renderer(rdata)
666676
inc kxi.runCount
667677
newtree.id = kxi.rootId

0 commit comments

Comments
 (0)