Skip to content Skip to sidebar Skip to footer

Listen To Javascript Events From Dart

Is there a way in Dart to listen for events from javascript libraries? For example the jqrangeslider http://ghusse.github.io/jQRangeSlider/ package emits a userValueChanged event w

Solution 1:

Thanks to js package you can deal with almost all js code. Here is the dart version of the following javascript code snippet :

$("#slider").bind("valuesChanged", function(e, data){
  console.log("Will be executed");
});

In Dart :

js.scoped((){
  js.context.jQuery("#slider").bind("valuesChanged", new js.Callback.many((e, data) {
    print("Will be executed");
  }));
});

Note that I use jQuery instead of $ because you would have issue with dart2js otherwise (see issue 2265).

Basically, when you use callback functions in JS, you have to use js.Callback.many or js.Callback.once to wrap your callback. See Managing callback lifetimes for more details.

Post a Comment for "Listen To Javascript Events From Dart"