4

I have a JavaScript library being imported in my HTML Documents head. How can I access objects from this library?

Thank you.

Siamak Motlagh
  • 5,028
  • 7
  • 41
  • 65
Nestor Ledon
  • 1,925
  • 2
  • 23
  • 36

1 Answers1

5

The interop with JavaScript is desribed in article 'Using JavaScript from Dart: The js Library'

In short, you have to:

//import the JS interop lib
import 'package:js/js.dart' as js;

// access the JS context for the page
var context = js.context;

// then use context to access JS object
var canvas = query('#map_canvas');
var googlemaps = js.context.google.maps;

// and create JS objects accessed through proxies
var googlemap = new js.Proxy(googlemaps.Map, canvas);

For more details (scopes, lifetimes, callbacks, etc) and examples, refer to linked article.

Zdeslav Vojkovic
  • 14,391
  • 32
  • 45
  • Thanks a ton! By any chance, would you know what the type is for js or context off the top of your head? I like to type strongly where I can. – Nestor Ledon Jun 02 '13 at 15:40
  • `js` is just an alias/namespace for the lib - it is not an object, and it is equal to whatever you put in `as js` part. you can also omit it and use `context` as a top level identifier. The type of context is `Proxy` (see http://dart-lang.github.io/js-interop/docs/js.html#context and https://github.com/dart-lang/js-interop/blob/master/lib/js.dart). For more info, see – Zdeslav Vojkovic Jun 02 '13 at 16:52