@dom
def chart(show: Var[Boolean]) = {
if(show.bind) {
<canvas id="chartCanvas"><canvas>
}
}
如何在加载到dom时使用某些图表库(如chartjs
)初始化画布?
答案 0 :(得分:4)
@dom
def chart(show: Var[Boolean]) = {
if(show.bind) {
val myCanvas = <canvas id="chartCanvas"><canvas>
myInitializationCode(myCanvas)
myCanvas
} else {
<!-- don't show canvas -->
}
}
您可以创建自定义SingleMountPoint,并将初始化代码放在重写mount
方法中:
val yourCustomMountPoint = new SingleMountPoint[Boolean](show) {
override def mount() = {
super.mount()
// Your custom initialization code
}
override def unmount() = {
// Your custom clean up code
super.unmount()
}
override def set(newValue: Boolean) = {
// Your custom handler when `show` get changed
}
}
// Inject your custom mount point into the rendering process
yourCustomMountPoint.bind