3

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.

1 Answer 1

4

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.

6
  • When I do what you have suggested using jQuery I get this error: The method 'jQuery' is not defined for the class 'Proxy'. Any suggestions? ps: same error if I use $ :-(
    – Softinio
    Sep 5, 2013 at 6:00
  • It's only a warning that will disappear in the next days. However the code should work. Sep 5, 2013 at 7:43
  • Code isn't working. I will post as an answer it would be fantastic if you can point me in the right direction. Thanks.
    – Softinio
    Sep 5, 2013 at 14:50
  • Don't you forget to load jquery.js ? Sep 5, 2013 at 14:52
  • I do , here is the details: stackoverflow.com/questions/18639922/…
    – Softinio
    Sep 5, 2013 at 15:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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