代码之家  ›  专栏  ›  技术社区  ›  TheKernel

斯卡拉。js引用:使用

  •  0
  • TheKernel  · 技术社区  · 8 年前

    我正在使用scala。js(v0.6.13)和Highcharts facade,我在试图访问一些变量时遇到了障碍,我通常会使用“this”和“chart”在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中的格式化程序和定位器函数中的“this.x”和“chart.plotWidth”。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]
    )
    

    编辑:图表属于highchart图表。

    1 回复  |  直到 8 年前
        1
  •  1
  •   sjrd    8 年前

    你需要使用 js.ThisFunctionN 要显式捕获特殊 this 将JavaScript作为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 这个 参数作为第一个参数传入。

    看见 https://www.scala-js.org/doc/interoperability/types.html 更多细节。

    对于 chart ,您的问题没有提供足够的上下文来了解 图表 在您的CoffeeScript代码中。但我想只是使用 图表 在斯卡拉。js将执行原始代码执行的任何操作。