Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 when someone releases the slider.

Is it easy to listen for that event in Dart via the js/js.dart package?

Would love to see some examples of a listener in Dart that listens for events like this from a javascript library.

like image 519
Douglas Fils Avatar asked Nov 17 '25 17:11

Douglas Fils


1 Answers

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.

like image 178
Alexandre Ardhuin Avatar answered Nov 19 '25 05:11

Alexandre Ardhuin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!