如何使用Binding.scala将元素加载到dom后执行一些init

时间:2017-07-05 05:44:35

标签: scala.js binding.scala

@dom 
def chart(show: Var[Boolean]) = {
  if(show.bind) {
    <canvas id="chartCanvas"><canvas>
  }
}

如何在加载到dom时使用某些图表库(如chartjs)初始化画布?

1 个答案:

答案 0 :(得分:4)

解决方案1 ​​

@dom 
def chart(show: Var[Boolean]) = {
  if(show.bind) {
    val myCanvas = <canvas id="chartCanvas"><canvas>
    myInitializationCode(myCanvas)
    myCanvas
  } else {
    <!-- don't show canvas -->
  }
}

解决方案2

您可以创建自定义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