mackristof
mackristof

Reputation: 61

import js file in ngComponent html template

I have a component which interact with JS library :

@NgComponent( selector: 'mapcomp', templateUrl: 'packages/org_rtosm_update/map/map.html', cssUrl: 'packages/org_rtosm_update/map/map.css', publishAs: 'mapComponent') class MapComponent { final map = new js.Proxy(js.context.OpenLayers.Map, "map");

when I include JS library in the HTML template file like this :

<script src="packages/browser/interop.js"></script>
<script type="text/javascript"src="http://openlayers.org/dev/OpenLayers.js"></script>
<h3>Maps</h3>
<div id="map" class="smallmap"></div>

I have Proxy error :

Class 'Proxy' has no instance getter 'OpenLayers'. NoSuchMethodError: method not found: 'OpenLayers' Receiver: Instance of 'Proxy' Arguments: [] STACKTRACE: #0
Object.noSuchMethod (dart:core-patch/object_patch.dart:45) #1
Proxy.noSuchMethod (package:js/js.dart:222:27) #2
MapComponent.MapComponent (package:org_rtosm_update/map/map_component.dart:15:39) #3 
_LocalClassMirror._invokeConstructor (dart:mirrors-patch/mirrors_impl.dart:873) #4
_LocalClassMirror.newInstance (dart:mirrors-patch/mirrors_impl.dart:789) #5
 DynamicInjector.newInstanceOf (package:di/dynamic_injector.dart:64:35) #6
_TypeProvider.get (package:di/module.dart:151:29) #7
 Injector._getInstanceByKey (package:di/injector.dart:116:27) #8
 Injector._getInstanceByKey (package:di/injector.dart:120:7) #9
 Injector.get (package:di/injector.dart:170:24) #10
_ComponentFactory.call (package:angular/core_dom/view_factory.dart:213:68)

what is the best way to have independant component with JS resources ?

thanks.

Upvotes: 0

Views: 427

Answers (1)

James deBoer
James deBoer

Reputation: 2495

Your specific problem is with the js.context.OpenLayers.Map call, to access properties on JSObjects, use the [] operator. e.g. js.context['OpenLayers']['Map'].

Using js-interop will work, but it is not very robust and you'll spend a lot of time writing Javascript through Dart which isn't very fun.

The better approach is to wrap the external Javascript library in a Dart library. Take a look at @a14n's wrapper generator: https://github.com/a14n/dart-js-wrapping-generator which is used to generate Google Map bindings: https://github.com/a14n/dart-google-maps

Upvotes: 1

Related Questions