How to get "this" from a Callback in Dart:js

dart, dart-js-interop

Solution

To capture `this` you need to use the `JsFunction.withThis()` constructor, which takes a closure whos first argument is the value of `this` in JavaScript.

See https://api.dartlang.org/apidocs/channels/stable/dartdoc-viewer/dart-js.JsFunction#id_JsFunction-withThis

Problem

I am trying to integrate highcharts as an AngularDart component. The Js interop works like a charm, but I have come across a problem I cannot overcome. Highcharts lets the user provide callbacks to customize some behaviours. For instance, a callback to format the tooltips of the chart. This is an example of formatter written in javascript: ``` formatter: function() { return 'The value for <b>'+ this.x + '</b> is <b>'+ this.y +'</b>'; } ``` I have my formatter function in Dart. The function gets called properly but I don't know how I could get "this" object to retrieve the data I need. Can anybody help me?

Original source