added base methods for VNodeRef
This commit is contained in:
parent
472f1deb58
commit
e33b9dc09a
3 changed files with 31 additions and 8 deletions
|
|
@ -1,16 +1,30 @@
|
||||||
include karaxprelude
|
include karaxprelude
|
||||||
import future, sequtils
|
import future, sequtils
|
||||||
|
|
||||||
|
type
|
||||||
|
CustomRef = ref object of VNodeRef
|
||||||
|
|
||||||
|
method onAttach(r: CustomRef) =
|
||||||
|
kout(cstring"custom ref attached")
|
||||||
|
|
||||||
|
method onDetach(r: CustomRef) =
|
||||||
|
kout(cstring"custom ref detached")
|
||||||
|
|
||||||
var
|
var
|
||||||
modelData = @[5, 2, 4]
|
modelData = @[5, 2, 4]
|
||||||
|
|
||||||
refA = VNodeRef()
|
refA = VNodeRef()
|
||||||
refB = VNodeRef()
|
refB = VNodeRef()
|
||||||
|
refC = CustomRef()
|
||||||
refSeq = newSeq[VNodeRef]()
|
refSeq = newSeq[VNodeRef]()
|
||||||
|
|
||||||
proc onClick(ev: Event, n: VNode) =
|
proc onClick(ev: Event, n: VNode) =
|
||||||
kout(refA.vnode)
|
modelData.add(0)
|
||||||
kout(refB.vnode)
|
|
||||||
|
proc showRefs() =
|
||||||
|
kout(refA)
|
||||||
|
kout(refB)
|
||||||
|
kout(refC)
|
||||||
kout(refSeq.map(nref => nref.vnode))
|
kout(refSeq.map(nref => nref.vnode))
|
||||||
|
|
||||||
proc secureRefSlot(i: int): VNodeRef =
|
proc secureRefSlot(i: int): VNodeRef =
|
||||||
|
|
@ -24,10 +38,13 @@ proc view(): VNode =
|
||||||
button(onclick=onClick):
|
button(onclick=onClick):
|
||||||
text "click me"
|
text "click me"
|
||||||
# storing refs to single elements is straightforward now
|
# storing refs to single elements is straightforward now
|
||||||
tdiv(nref=refA):
|
if modelData.len mod 2 == 0:
|
||||||
text "A"
|
tdiv(nref=refA):
|
||||||
tdiv(nref=refB):
|
text "A"
|
||||||
text "B"
|
tdiv(nref=refB):
|
||||||
|
text "B"
|
||||||
|
tdiv(nref=refC):
|
||||||
|
text "C"
|
||||||
# It's a bit more tricky when containers are involved:
|
# It's a bit more tricky when containers are involved:
|
||||||
for i, x in modelData.pairs:
|
for i, x in modelData.pairs:
|
||||||
tdiv(nref=secureRefSlot(i)):
|
tdiv(nref=secureRefSlot(i)):
|
||||||
|
|
@ -36,4 +53,4 @@ proc view(): VNode =
|
||||||
proc renderer(): VNode =
|
proc renderer(): VNode =
|
||||||
view()
|
view()
|
||||||
|
|
||||||
setRenderer renderer
|
setRenderer(renderer, showRefs)
|
||||||
|
|
|
||||||
|
|
@ -68,10 +68,12 @@ proc wrapEvent(d: Node; n: VNode; k: EventKind; action: EventHandler) =
|
||||||
template detach(n: VNode) =
|
template detach(n: VNode) =
|
||||||
if not n.nref.isNil():
|
if not n.nref.isNil():
|
||||||
n.nref.vnode = nil
|
n.nref.vnode = nil
|
||||||
|
n.nref.onDetach()
|
||||||
n.dom = nil
|
n.dom = nil
|
||||||
template attach(n: VNode) =
|
template attach(n: VNode) =
|
||||||
if not n.nref.isNil():
|
if not n.nref.isNil():
|
||||||
n.nref.vnode = n
|
n.nref.vnode = n
|
||||||
|
n.nref.onAttach()
|
||||||
n.dom = result
|
n.dom = result
|
||||||
|
|
||||||
proc vnodeToDom(n: VNode): Node =
|
proc vnodeToDom(n: VNode): Node =
|
||||||
|
|
|
||||||
|
|
@ -101,7 +101,7 @@ macro buildLookupTables(): untyped =
|
||||||
buildLookupTables()
|
buildLookupTables()
|
||||||
|
|
||||||
type
|
type
|
||||||
VNodeRef* = ref object
|
VNodeRef* = ref object of RootObj
|
||||||
vnode*: VNode
|
vnode*: VNode
|
||||||
|
|
||||||
EventHandler* = proc (ev: Event; target: VNode) {.closure.}
|
EventHandler* = proc (ev: Event; target: VNode) {.closure.}
|
||||||
|
|
@ -122,6 +122,10 @@ type
|
||||||
dom*: Node ## the attached real DOM node. Can be 'nil' if the virtual node
|
dom*: Node ## the attached real DOM node. Can be 'nil' if the virtual node
|
||||||
## is not part of the virtual DOM anymore.
|
## is not part of the virtual DOM anymore.
|
||||||
|
|
||||||
|
method onAttach*(v: VNodeRef) {.base.} = discard
|
||||||
|
method onDetach*(v: VNodeRef) {.base.} = discard
|
||||||
|
method changed*(v: VNodeRef): bool {.base.} = false
|
||||||
|
|
||||||
proc value*(n: VNode): cstring = n.text
|
proc value*(n: VNode): cstring = n.text
|
||||||
proc `value=`*(n: VNode; v: cstring) = n.text = v
|
proc `value=`*(n: VNode; v: cstring) = n.text = v
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue