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

Issue

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?

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

Answered By – Justin Fagnani

Answer Checked By – Clifford M. (FlutterFixes Volunteer)

Leave a Reply

Your email address will not be published.