Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Question: Need clarification on behaviour of newComponent() #135

Open
TheSimpleZ opened this issue Mar 9, 2020 · 2 comments
Open

Question: Need clarification on behaviour of newComponent() #135

TheSimpleZ opened this issue Mar 9, 2020 · 2 comments
Labels

Comments

@TheSimpleZ
Copy link

Hello!

I'm trying to create a component that wraps xterm.js. To initialize the terminal I need to call a few JS functions after a div has been attached to the DOM.

I was under the impression that the onAttach parameter of newComponent() was meant for this purpose, but I think i'm wrong. As seen here, the onAttachImpl proc is called before renderImpl:

https://github.com/pragmagic/karax/blob/db4acbf7d64549196b58928bb51c15252482f91c/karax/karax.nim#L162-L165

I believe this causes the onAttach to be fired before the element actually exists. Is this on purpose? If yes, is there any way of knowing when an element has been fully loaded into the DOM?

Essentially, this is the code i'm using:

include karax / [prelude, vstyles, kdom]
import jsffi
import sugar

var Terminal {.importjs: "new Terminal()", nodecl.}: JsObject

type termi = ref object of VComponent

proc render(x: VComponent): VNode =
  echo "rendered"
  result = buildHtml:
    tdiv(id = "terminal"):
      text "hello"

proc loaded(x: VComponent) =
  # This doesn't work because "terminal" doesn't exist yet
  let thediv = getElementById(kstring"terminal") 

  # Initialize xterm.js
  let term = Terminal
  term.open(thediv) # This fails because thediv doesn't exist
  term.write(r"Hello from \x1B[1;3;31mxterm.js\x1B[0m $ ")
  echo "loaded"

proc createDom(): VNode =
  result = buildHtml(tdiv):
    newComponent(termi, render, loaded)

setRenderer createDom
@TheSimpleZ
Copy link
Author

TheSimpleZ commented Mar 9, 2020

Actually, nevermind. I figured out that I'm looking in the wrong place. setRenderer has a parameter called clientPostRenderCallback that is used for this purpose.

Edit: Actually, no. That only works for the root component since that's the only with with a setRenderer. Is it possible to get similar behavior on a component level or do i have to expose an init() proc from each component that i can call in the setRenderer callback?

@TheSimpleZ TheSimpleZ reopened this Mar 9, 2020
@dawkot
Copy link
Contributor

dawkot commented Apr 9, 2020

AFAIK components have been completely broken for some time now, anyway.

@ajusa ajusa added the question label May 2, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants