17

My company has a very buggy fat client application written in JavaScript. As usual with a large JavaScript application, the code is rapidly becoming unmanageable.

I personally believe that writing in Dart would be a much better solution. But the 'start again' approach to management will not work.

I known that one can call JavaScript code from Dart, but is it possible to call Dart code from JavaScript?

This would allow us to incrementally replace the more critical libraries with Dart versions and still be able to use the original code base.

ggorlen
  • 44,755
  • 7
  • 76
  • 106
richard
  • 2,887
  • 5
  • 26
  • 36
  • Does this answer your question? [How do I create a global (on the window) object in Dart lang?](https://stackoverflow.com/questions/45812372/how-do-i-create-a-global-on-the-window-object-in-dart-lang) – ggorlen Dec 09 '20 at 19:31

3 Answers3

19

You should try Dart's JS interop library.

You can define a callback function inside Dart and export it (in a way) to JavaScript.

Take a look at this example:

Dart code defines JS function

 context['javascriptFunctionName'] = (parameter) {
       //call any Dart method
    }

then call it from JavaScript:

javascriptFunctionName({'param': 'value'});
johntellsall
  • 14,394
  • 4
  • 46
  • 40
Pawel Uchida-Psztyc
  • 3,735
  • 2
  • 20
  • 41
  • 2
    You can find a simple example project of this at https://github.com/hangstrap/TestJavascriptCall Thanks for the help! – richard Apr 28 '14 at 08:25
  • Note: This `context[...` thing is to be added on dart's `main()` body. And you need also a `import 'dart:js';` – alanjds Jun 26 '19 at 00:39
  • Hey, can we pass parameters from javascript to dart. I am able to call a dart function from javascript but not able to pass parameters with the function. – Ayush Malviya Jul 13 '20 at 06:46
  • @AyushMalviya in my example you have a way of defining an export (from Dart) function to JavaScript and how to call it with parameters. I am not sure how accurate it is right now as I wasn't using Dart for some time but I would assume it still works the same. – Pawel Uchida-Psztyc Jul 14 '20 at 07:52
6

With package js instead of dart:js it can be made available to JS this way:

import 'dart:html';
import 'package:js/js_util.dart';

void main() {
  setProperty(window, 'callDartFunc', allowInterop(dartFunc));
}

String dartFunc() => 'Hello World';

Thanks to Matan Lurey https://gitter.im/dart-lang/TALK-general?at=585b9b42db9cafe9183a3345

Günter Zöchbauer
  • 623,577
  • 216
  • 2,003
  • 1,567
-5

As long as you include your mandatory dart lines in your html files:

<script type="application/dart" src="fileName.dart"></script>
<script src="packages/browser/dart.js"></script>

you should be able add the dart replacement code and compile it. This way you may be able to start converting over bit by bit. To the best of my knowledge there is no way to call dart code from JavaScript though.


Now that I have a little more time, let me expound a bit. Since Dart is a relatively new language, along with the fact that it doesn't so much add anything to the JavaScript language, there isn't a lot of incentive for Oracle to add cross-compatibility with Dart. Though the two languages may not so much be cross-compatible, they can both coexist together and have a more one way relationship.

As long as you are calling your dart code directly from your html files or from other dart functions, you can at least start the process of integrating dart into your site. Then as time allows, you should be able to start the long process of updating your JavaScript to dart, if that is the direction you decide to take.

Jay
  • 37
  • 3
  • Thanks, I guess that running Dart and Javascript in the same page is a start, but I would need to be able to call Dart Functions from Javascript code. Our application is far more complex that a few javascript or dart functions hanging off a few dom elements... – richard Apr 19 '14 at 07:00
  • 8
    What has Oracle to do with this all? – Günter Zöchbauer Apr 22 '14 at 06:46