Will Dart support the use of existing JavaScript libraries?

JavascriptLibrariesDart

Javascript Problem Overview


I understand Dart compiles to JavaScript, and I read the Dart Language Spec on Libraries, although I didn't see an answer there. Also a search on their discussion form for the word 'existing' turns up 3 results that are not related.

Does anyone know if Dart will support the use of existing JavaScript libraries such as jQuery or Raphael?

Javascript Solutions


Solution 1 - Javascript

The answer is now Yes! Dart now ships a JS-interop library to use existing JavaScript code with your Dart app. Learn more here: https://www.dartlang.org/articles/js-dart-interop/

Solution 2 - Javascript

You will not be able to call javascript directly from dart code. The native directive is reserved for the core libraries of dartc (dart:core, dart:dom, dart:html, dart:json, etc), which itself compiles to javascript.

Solution 3 - Javascript

There is now a new simpler way https://pub.dartlang.org/packages/js (currently version 0.6.0-beta.6)

Make JS classes and functions available to Dart like:

@JS("JSON.stringify")
external String stringify(obj);

@JS('google.maps')
library maps;

// Invokes the JavaScript getter `google.maps.map`.
external Map get map;

// `new Map` invokes JavaScript `new google.maps.Map(location)`
@JS()
class Map {
  external Map(Location location);
  external Location getLocation();
}

// `new Location(...)` invokes JavaScript `new google.maps.LatLng(...)`
//
// We recommend against using custom JavaScript names whenever
// possible. It is easier for users if the JavaScript names and Dart names
// are consistent.
@JS("LatLng")
class Location {
  external Location(num lat, num lng);
}

for more see the readme of the package

Solution 4 - Javascript

There is also a dart:js library. And here is an article explaining how to use this library for interoperating with JavaScript.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionTMBView Question on Stackoverflow
Solution 1 - JavascriptSeth LaddView Answer on Stackoverflow
Solution 2 - JavascriptjtmcdoleView Answer on Stackoverflow
Solution 3 - JavascriptGünter ZöchbauerView Answer on Stackoverflow
Solution 4 - JavascriptShailen TuliView Answer on Stackoverflow