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
|
||||
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
|
||||
modelData = @[5, 2, 4]
|
||||
|
||||
refA = VNodeRef()
|
||||
refB = VNodeRef()
|
||||
refC = CustomRef()
|
||||
refSeq = newSeq[VNodeRef]()
|
||||
|
||||
proc onClick(ev: Event, n: VNode) =
|
||||
kout(refA.vnode)
|
||||
kout(refB.vnode)
|
||||
modelData.add(0)
|
||||
|
||||
proc showRefs() =
|
||||
kout(refA)
|
||||
kout(refB)
|
||||
kout(refC)
|
||||
kout(refSeq.map(nref => nref.vnode))
|
||||
|
||||
proc secureRefSlot(i: int): VNodeRef =
|
||||
|
|
@ -24,10 +38,13 @@ proc view(): VNode =
|
|||
button(onclick=onClick):
|
||||
text "click me"
|
||||
# storing refs to single elements is straightforward now
|
||||
tdiv(nref=refA):
|
||||
text "A"
|
||||
tdiv(nref=refB):
|
||||
text "B"
|
||||
if modelData.len mod 2 == 0:
|
||||
tdiv(nref=refA):
|
||||
text "A"
|
||||
tdiv(nref=refB):
|
||||
text "B"
|
||||
tdiv(nref=refC):
|
||||
text "C"
|
||||
# It's a bit more tricky when containers are involved:
|
||||
for i, x in modelData.pairs:
|
||||
tdiv(nref=secureRefSlot(i)):
|
||||
|
|
@ -36,4 +53,4 @@ proc view(): VNode =
|
|||
proc renderer(): VNode =
|
||||
view()
|
||||
|
||||
setRenderer renderer
|
||||
setRenderer(renderer, showRefs)
|
||||
|
|
|
|||
|
|
@ -68,10 +68,12 @@ proc wrapEvent(d: Node; n: VNode; k: EventKind; action: EventHandler) =
|
|||
template detach(n: VNode) =
|
||||
if not n.nref.isNil():
|
||||
n.nref.vnode = nil
|
||||
n.nref.onDetach()
|
||||
n.dom = nil
|
||||
template attach(n: VNode) =
|
||||
if not n.nref.isNil():
|
||||
n.nref.vnode = n
|
||||
n.nref.onAttach()
|
||||
n.dom = result
|
||||
|
||||
proc vnodeToDom(n: VNode): Node =
|
||||
|
|
|
|||
|
|
@ -101,7 +101,7 @@ macro buildLookupTables(): untyped =
|
|||
buildLookupTables()
|
||||
|
||||
type
|
||||
VNodeRef* = ref object
|
||||
VNodeRef* = ref object of RootObj
|
||||
vnode*: VNode
|
||||
|
||||
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
|
||||
## 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; v: cstring) = n.text = v
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue