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.
Thanks
Richard
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'});
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
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.