Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I know how Dart to use other javascript through dart.js library. How do I use Dart generated JavaScript in other JavaScript? How do I call a function in Dart generated javascript?

share|improve this question
add comment

1 Answer

up vote 3 down vote accepted

Using JS interop, you can call Javascript from Dart, and Dart from Javascript.

To call Dart from Javascript, you first need to register the functions that you want Javascript to have access to. It's also possible to pass primitives between JS and Dart and vice-versa.

import 'dart:html';
import 'dart:js' as js;

void main() {
  js.context["myFunc"] = () {
    print("called from javascript");
  };

  js.context["sayGreeting"] = (message) {
    print("greeting: $message");
  };
}

Then from Javascript, you can call the functions that you registered:

myFunc(); // prints 'called from javascript'
sayGreeting("Hello"); // prints 'greeting: Hello'
share|improve this answer
    
Great! Thanks for the help. –  kzhdev Mar 5 at 13:48
    
Exporting dart function in Javascript worked, but the exposed function might conflict with a function from other library (have same name). It is possible to warp those exported functions into a Javascript object? So in Javascript I can call myClass.myFunc(). –  kzhdev Mar 28 at 21:02
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.