我使用的是scala.js (v0.6.13)和高级图表外观,我遇到了一个障碍,试图访问我通常使用'this‘和’图表‘在javascript中访问的一些变量。下面是一个例子:
咖啡脚本:
tooltip: {
enabled: true,
positioner: (labelWidth, labelHeight, point) ->
return { x: chart.plotWidth - labelWidth + chart.plotLeft, y: 17 }
formatter: () ->
x = this.x
point = this.points.find (p) -> x == p.x
...我的问题是如何在scala.js中访问格式化程序和定位器函数中的“this.x.x”和“scala.js”?到目前为止,以下是我的scala代码:
override val tooltip: Cfg[Tooltip] = Tooltip(
formatter = { () =>
"what?"
}: js.Function0[String],
positioner = { (labelWidth: Any, labelHeight: Any, point: Object) =>
js.Dynamic.literal(
x = labelWidth,
y = 17)
}: js.Function3[Any, Any, Object, Object]
)编辑:图表属于高图表。
发布于 2016-12-05 07:05:07
您需要使用js.ThisFunctionN显式地捕获JavaScript的特殊this作为Scala.js中的正常参数。
positioner = { (thiz: js.Dynamic, labelWidth: Any, labelHeight: Any, point: Object) =>
// here the variable `thiz` holds what would be `this` in JS
...
}: js.ThisFunction3[js.Dynamic, Any, Any, Object, Object]当将Scala匿名函数转换为js.ThisFunction时,this参数作为第一个参数传入。
有关更多详细信息,请参阅https://www.scala-js.org/doc/interoperability/types.html。
对于chart,您的问题没有给出足够的上下文来了解CoffeeScript代码中的chart。但是我想,在chart中使用Scala.js就可以完成原始代码所做的任何事情。
https://stackoverflow.com/questions/40968262
复制相似问题