OLD | NEW |
1 The package now only contains annotations specifying the shape of the | 1 Methods and annotations to specify interoperability with JavaScript APIs. |
2 JavaScript API to import into Dart. | |
3 | 2 |
4 The core implementation is defined directly in Dart2Js, Dartium, and dev_compile
r. | 3 *Note: This package is beta software.* |
| 4 |
| 5 *Note: This packages requires Dart SDK `>=1.13.0-dev.7`.* |
| 6 |
| 7 ### Adding the dependency |
| 8 |
| 9 Add the following to your `pubspec.yaml`: |
| 10 |
| 11 ```yaml |
| 12 dependencies: |
| 13 js: ^0.6.0-beta |
| 14 ``` |
| 15 |
| 16 #### Passing functions to JavaScript. |
| 17 |
| 18 If you are passing a Dart function to a JavaScript API, you must wrap it using |
| 19 `allowInterop` or `allowInteropCaptureThis`. |
| 20 |
| 21 ### Examples |
| 22 |
| 23 #### Calling methods |
| 24 |
| 25 ```dart |
| 26 // Calls invoke JavaScript `JSON.stringify(obj)`. |
| 27 @Js("JSON.stringify") |
| 28 external String stringify(obj); |
| 29 ``` |
| 30 |
| 31 #### Classes and Namespaces |
| 32 |
| 33 ```dart |
| 34 @Js('google.maps') |
| 35 library maps; |
| 36 |
| 37 // Invokes the JavaScript getter `google.maps.map`. |
| 38 external Map get map; |
| 39 |
| 40 // `new Map` invokes JavaScript `new google.maps.Map(location)` |
| 41 @Js() |
| 42 class Map { |
| 43 external Map(Location location); |
| 44 external Location getLocation(); |
| 45 } |
| 46 |
| 47 // `new Location(...)` invokes JavaScript `new google.maps.LatLng(...)` |
| 48 // |
| 49 // We recommend against using custom JavaScript names whenever |
| 50 // possible. It is easier for users if the JavaScript names and Dart names |
| 51 // are consistent. |
| 52 @Js("LatLng") |
| 53 class Location { |
| 54 external Location(num lat, num lng); |
| 55 } |
| 56 ``` |
| 57 |
| 58 #### Maps |
| 59 |
| 60 Dart `Map` objects, including literals, are "opaque" in JavaScript. |
| 61 You must create Dart classes for each of these. |
| 62 |
| 63 ```js |
| 64 // JavaScript |
| 65 printOptions({responsive: true}); |
| 66 ``` |
| 67 |
| 68 ```dart |
| 69 // Dart |
| 70 void main() { |
| 71 printOptions(new Options(responsive: true)); |
| 72 } |
| 73 |
| 74 @Js() |
| 75 external printOptions(Options options); |
| 76 |
| 77 @Js() |
| 78 class Options { |
| 79 external bool get responsive; |
| 80 |
| 81 external factory Options({bool responsive}); |
| 82 } |
| 83 ``` |
5 | 84 |
6 ## Contributing and Filing Bugs | 85 ## Contributing and Filing Bugs |
7 | 86 |
8 Please file bugs and features requests on the [Github issue tracker](https://git
hub.com/dart-lang/js-interop/issues). | 87 Please file bugs and features requests on the [Github issue tracker](https://git
hub.com/dart-lang/js-interop/issues). |
9 | 88 |
10 We also love and accept community contributions, from API suggestions to pull re
quests. | 89 We also love and accept community contributions, from API suggestions to pull re
quests. |
11 Please file an issue before beginning work so we can discuss the design and impl
ementation. | 90 Please file an issue before beginning work so we can discuss the design and impl
ementation. |
12 We are trying to create issues for all current and future work, so if something
there intrigues you (or you need it!) join in on the discussion. | 91 We are trying to create issues for all current and future work, so if something
there intrigues you (or you need it!) join in on the discussion. |
13 | 92 |
14 All we require is that you sign the | 93 Code contributors must sign the |
15 [Google Individual Contributor License Agreement](https://developers.google.com/
open-source/cla/individual?csw=1). | 94 [Google Individual Contributor License Agreement](https://developers.google.com/
open-source/cla/individual?csw=1). |
OLD | NEW |