2017-07-05 42 views
1
@dom 
def chart(show: Var[Boolean]) = { 
    if(show.bind) { 
    <canvas id="chartCanvas"><canvas> 
    } 
} 

如何在启动画布时使用一些图表库,如chartjs将它加载到dom时?如何在元素加载到dom后执行一些init Binding.scala

+0

你可能有一个看看使用Binding.scala与d3.js https:// githu的其他人的代码b.com/emanresusername/word-cloud-generator/blob/master/ui/src/main/scala/my/will/be/done/wordcloud/component/WordcloudComponent.scala –

+0

@YangBo我不认为代码解决我的问题。看看这个例子,如果'show'发生变化,我需要重绘图表。所以我认为云可以通过binding.scala的事件监听器来完成。 – jilen

+0

尝试将这些重绘代码放在if区块中 –

回答

3

解决方案1 ​​

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

解决方案2

您可以创建自定义SingleMountPoint,并把初始化代码的重写mount方法:

val yourCustomMountPoint = new SingleMountPoint[Boolean](show) { 
    override def mount() = { 
    super.mount() 
    // You custom initialization code 
    } 
    override def unmount() = { 
    // You custom clean up code 
    super.unmount() 
    } 
    override def set(newValue: Boolean) = { 
    // You custom handler when `show` get changed 
    } 
} 

// Inject your custom mount point into the rendering process 
yourCustomMountPoint.bind 
+0

谢谢。这为我补充了缺失的部分。 – jens

相关问题