1
votes

I am trying to access existing javascript libraries from within an angular-dart application. Which is the recommended way to do that? Is something like dart:js available?

Thanks in advance for any ideas.

EDIT: did some tests. It appears that dart:js doesn't mind being imported in angular.dart (btw, should I be importing 'dart:js' or 'package:js/js.dart'? what is the difference, if any?).

Since in angular you import javascript libraries through modules and in dart through dart:js, it would be great if angular-dart's documentation could address clearly (maybe with some examples?) this issue that must be of interest to many people.

1
The angularjs tag should probably be removed as that is not applicable here. Also, consider expanding your question to include the libraries you want to access and more broadly, what you are trying to accomplish.Fourth
(1) I am afraid there is no way (yet) to do what I'm asking for in angular-dart, so I am tagging 'angular' in the (admittedly, remote) case there is some other, non-dart-based way to do it (2) The question is about the general approach, not any specific library.alearg

1 Answers

3
votes

There is library name exactly dart:js Its quite good in accessing data and calling javascript functions from dart. Just use context['property'] or context['property'].callMethod(name, [args]).

Communication from javascript to dart is now possible only by DOM or callbacks. First is about setting some elements attribute (or class) and detect changes in dart. Callbacks work much better, you need to call javascript function from dart passing function in arguments.

//js
function bindEvent ( eventName, cb )
{
    //call dart function
    cb( "Triggering event: " + eventName );
}
//dart
void onJsEvent( Object o, String message )
{
    print("Callback called with this = $o and message = $message");
}

var callback = new JsFunction.withThis( onJsEvent );
context.callMethod("bindEvent", [ "myEvent", callback ]);

I think you will have hard time mapping angular, since it uses a lot of custom objects. Dart-Js communication works good only with primitives, maps {} and arrays [].