OLD | NEW |
1 Methods and annotations to specify interoperability with JavaScript APIs. | 1 Methods and annotations to specify interoperability with JavaScript APIs. |
2 | 2 |
3 *Note: This package is beta software.* | 3 *Note: This package is beta software.* |
4 | 4 |
5 *Note: This packages requires Dart SDK `>=1.13.0-dev.7`.* | 5 *Note: This packages requires Dart SDK `>=1.13.0-dev.7`.* |
6 | 6 |
7 ### Adding the dependency | 7 ### Adding the dependency |
8 | 8 |
9 Add the following to your `pubspec.yaml`: | 9 Add the following to your `pubspec.yaml`: |
10 | 10 |
11 ```yaml | 11 ```yaml |
12 dependencies: | 12 dependencies: |
13 js: ^0.6.0-beta | 13 js: ^0.6.0-beta |
14 ``` | 14 ``` |
15 | 15 |
16 #### Passing functions to JavaScript. | 16 #### Passing functions to JavaScript. |
17 | 17 |
18 If you are passing a Dart function to a JavaScript API, you must wrap it using | 18 If you are passing a Dart function to a JavaScript API, you must wrap it using |
19 `allowInterop` or `allowInteropCaptureThis`. | 19 `allowInterop` or `allowInteropCaptureThis`. |
20 | 20 |
21 ### Examples | 21 ### Examples |
22 | 22 |
23 There is a [full example](https://github.com/dart-lang/sdk/tree/master/pkg/js/ex
ample) hosted with the `package:js` source code. | 23 There is a [full example](https://github.com/dart-lang/sdk/tree/master/pkg/js/ex
ample) hosted with the `package:js` source code. |
24 | 24 |
25 #### Calling methods | 25 #### Calling methods |
26 | 26 |
27 ```dart | 27 ```dart |
28 // Calls invoke JavaScript `JSON.stringify(obj)`. | 28 // Calls invoke JavaScript `JSON.stringify(obj)`. |
29 @Js("JSON.stringify") | 29 @JS("JSON.stringify") |
30 external String stringify(obj); | 30 external String stringify(obj); |
31 ``` | 31 ``` |
32 | 32 |
33 #### Classes and Namespaces | 33 #### Classes and Namespaces |
34 | 34 |
35 ```dart | 35 ```dart |
36 @Js('google.maps') | 36 @JS('google.maps') |
37 library maps; | 37 library maps; |
38 | 38 |
39 // Invokes the JavaScript getter `google.maps.map`. | 39 // Invokes the JavaScript getter `google.maps.map`. |
40 external Map get map; | 40 external Map get map; |
41 | 41 |
42 // `new Map` invokes JavaScript `new google.maps.Map(location)` | 42 // `new Map` invokes JavaScript `new google.maps.Map(location)` |
43 @Js() | 43 @JS() |
44 class Map { | 44 class Map { |
45 external Map(Location location); | 45 external Map(Location location); |
46 external Location getLocation(); | 46 external Location getLocation(); |
47 } | 47 } |
48 | 48 |
49 // `new Location(...)` invokes JavaScript `new google.maps.LatLng(...)` | 49 // `new Location(...)` invokes JavaScript `new google.maps.LatLng(...)` |
50 // | 50 // |
51 // We recommend against using custom JavaScript names whenever | 51 // We recommend against using custom JavaScript names whenever |
52 // possible. It is easier for users if the JavaScript names and Dart names | 52 // possible. It is easier for users if the JavaScript names and Dart names |
53 // are consistent. | 53 // are consistent. |
54 @Js("LatLng") | 54 @JS("LatLng") |
55 class Location { | 55 class Location { |
56 external Location(num lat, num lng); | 56 external Location(num lat, num lng); |
57 } | 57 } |
58 ``` | 58 ``` |
59 | 59 |
60 #### Maps | 60 #### JavaScript object literals |
61 | 61 |
62 Dart `Map` objects, including literals, are "opaque" in JavaScript. | 62 Many JavaScript APIs take an object literal as an argument. For example: |
63 You must create Dart classes for each of these. | |
64 | |
65 ```js | 63 ```js |
66 // JavaScript | 64 // JavaScript |
67 printOptions({responsive: true}); | 65 printOptions({responsive: true}); |
68 ``` | 66 ``` |
69 | 67 |
| 68 If you want to use `printOptions` from Dart, you cannot simply pass a Dart `Map` |
| 69 object – they are are "opaque" in JavaScript. |
| 70 |
| 71 |
| 72 Instead, create a Dart class with both the `@JS()` and |
| 73 `@anonymous` annotations. |
| 74 |
70 ```dart | 75 ```dart |
71 // Dart | 76 // Dart |
72 void main() { | 77 void main() { |
73 printOptions(new Options(responsive: true)); | 78 printOptions(new Options(responsive: true)); |
74 } | 79 } |
75 | 80 |
76 @Js() | 81 @JS() |
77 external printOptions(Options options); | 82 external printOptions(Options options); |
78 | 83 |
79 @Js() | 84 @JS() |
| 85 @anonymous |
80 class Options { | 86 class Options { |
81 external bool get responsive; | 87 external bool get responsive; |
82 | 88 |
83 external factory Options({bool responsive}); | 89 external factory Options({bool responsive}); |
84 } | 90 } |
85 ``` | 91 ``` |
86 | 92 |
87 ## Contributing and Filing Bugs | 93 ## Contributing and Filing Bugs |
88 | 94 |
89 Please file bugs and features requests on the [Github issue tracker](https://git
hub.com/dart-lang/js-interop/issues). | 95 Please file bugs and features requests on the [Github issue tracker](https://git
hub.com/dart-lang/sdk/issues). |
90 | 96 |
91 We also love and accept community contributions, from API suggestions to pull re
quests. | 97 We also love and accept community contributions, from API suggestions to pull re
quests. |
92 Please file an issue before beginning work so we can discuss the design and impl
ementation. | 98 Please file an issue before beginning work so we can discuss the design and impl
ementation. |
93 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. | 99 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. |
94 | 100 |
95 Code contributors must sign the | 101 Code contributors must sign the |
96 [Google Individual Contributor License Agreement](https://developers.google.com/
open-source/cla/individual?csw=1). | 102 [Google Individual Contributor License Agreement](https://developers.google.com/
open-source/cla/individual?csw=1). |
OLD | NEW |