0

I'm using scala.js (v0.6.13) with the Highcharts facade, and I have run into a roadblock trying to access some variables that I would normally access in javascript using 'this' and 'chart'. Here's an example:

coffeescript:

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
    ...

My question is how do I access "this.x" and "chart.plotWidth" in my formatter and positioner functions in scala.js? Here's my scala code thus far:

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]
)

edit: chart pertains to a highchart chart.

1
  • You might want to point out that your original snippet is written in CoffeeScript rather than JavaScript, so that Scala.js readers not familiar with CoffeeScript don't get utterly confused. Commented Dec 5, 2016 at 7:06

1 Answer 1

1

You need to use a js.ThisFunctionN to explicitly capture the special this of JavaScript as a normal parameter in 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]

When converting a Scala anonymous function to a js.ThisFunction, the this argument is passed in as the first parameter.

See https://www.scala-js.org/doc/interoperability/types.html for more details.

For chart, your question does not give enough context to know what chart is in your CoffeeScript code. But I guess just using chart in Scala.js will do whatever the original code does.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, for the direction!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.