1

I want to write some Dart function "oKey" which calls JavaScript function "jsOnKey" (with success or exception too since cannot predict).

Next I want that JavaScript function "onKey" will call Dart function "callbackFromJs" to return control to Dart again (with success or exception).

Can you help me with this full flow - please assume SUCCESS or EXCEPTION on each border - I can not rely on 3rd party code - DART 2 JS 2 DART?


To make more context to this general question I put example code.

import 'dart:html';

void onKey(Event event) {
  // I want to call something in javascript
  // function callbackFromDart () { 
  //  /* something */; 
  //  /* call callbackJs in Dart - return control to dart */
  // }
}

void callbackFromJs() {
  // It should be called from JavaScript
}

void main() {
  InputElement nameElement = querySelector('input[name=name]');
  nameElement..placeholder = 'Enter text'
      ..onKeyUp.listen(onKey);

  InputElement descriptionElement = querySelector('input[name=description]');
  descriptionElement..placeholder = 'Enter text'
    ..onKeyUp.listen(onKey);
}
Günter Zöchbauer
  • 623,577
  • 216
  • 2,003
  • 1,567
Chameleon
  • 9,722
  • 16
  • 65
  • 127

1 Answers1

2

First have a look at Using JavaScript from Dart.

For your case you can simply pass callbacks to handle what you call Js 2 Dart :

import 'dart:js' as js;

void onKey(Event event) {
  onSuccess() {
    // Dart callback called from Js
  }
  onError() {
    // Dart callback called from Js
  }

  // assuming your js function takes 2 callbacks as parameters
  try {
    // in JS : function a() { throw "throw from js"; }
    js.context.callMethod('myTopLevelFunction', [onSuccess, onError]);
  } 
  catch (e) {
    print('js error catch on Dart side : $e');
  }
}

The Dart exceptions can be catch with the same kind of code on Js side.

Chameleon
  • 9,722
  • 16
  • 65
  • 127
Alexandre Ardhuin
  • 71,959
  • 15
  • 151
  • 132
  • It is not answer. Call chain is Dart 2 Js 2 Dart (not Dart 2 Js). The problem is how to handle JavaScript exception in Dart and Dart exception in JavaScript (not onSuccess, onError). – Chameleon Feb 10 '14 at 22:23
  • If you follow my example there are dart to js call (call to `myTopLevelFunction`) **and** js to Dart call (when callbacks are called from js). – Alexandre Ardhuin Feb 11 '14 at 08:14
  • Let's check your answer readiness: 1. d2js done, 2. d2js exception - partially done - not shown how to catch specific exception type. 3. js2d - done(I imagine code). 4. js2d exception - not done (not code catching specific exception). 50% is done (only exception types is left). – Chameleon Feb 11 '14 at 12:07
  • Please provide how to catch specific exception type to make your answer very good - I will score it and many people will be thankful for showing catching **known** exception type and catch **any/unknown** exception. – Chameleon Feb 11 '14 at 12:09
  • I agree, this doesn't answer the question. You can't expect to always pass a callback to call the Dart function. It should be callable directly...and I don't exactly see how anywhere. Very frustrating. – Tom Mar 04 '14 at 22:24
  • http://stackoverflow.com/questions/21936318/how-to-call-a-dart-function-from-javascript would show you how to do it straight up. – Tom Mar 04 '14 at 23:12