robbie
robbie

Reputation: 269

Dart SDK 0.8.10.3_r29803 dart:js callbacks

Can some one please give me an example of the Dart code that would follow this flow

1) Dart call Javascript function 2) Javascript loads some data via Javascript api's 3) Javascript returns data to Dart

Currently I can only call the Javascript function I need (via js.context.callMethod('myAmazingFunction');) but I can't receive the callback. I thought there would be something like js.context.listenForMethod('myAmazingCallback'); or similar...

Upvotes: 2

Views: 498

Answers (1)

Justin Fagnani
Justin Fagnani

Reputation: 11201

Just pass your Dart function into JavaScript and it'll automatically converted to a JavaScript function.

Dart:

import 'dart:js';

myCallback(data) {
  print('received $data');
}

main() {
  context.callMethod('mJsFunction', [myCallback]);
}

JS:

function myJsFunction(callback) {
  callback('some data');
}

For data passed to the Dart callback, many types will be automatically converted (see the list here: http://api.dartlang.org/docs/releases/latest/dart_js.html) and other types will give you a JsObject proxy.

Upvotes: 4

Related Questions