I'm new to Dart and I'm having trouble getting started with the js-interop library. I want to add a slider from jquery ui to my page, but I cannot figure out how to do the slider() setup call from Dart. Is js-interop the correct way of doing this? Some help with this would be much appreciated.
void main() {
  js.scoped(() {
    var slider = query('#slider-range');
    var options = js.map({
      'range': true,
      'values': [ 17, 67 ]
    });
    // This doesn't work. Element has no method named slider.
    slider.slider(options);
    // In javascript it's done like this:
    // $( "#slider-range" ).slider({
    //   range: true,
    //   values: [ 17, 67 ]
    // });
    // This alert works.
    js.context.alert('Hello from Dart via JS');
  });
}
                In your case, you have to use js.context.$('#slider-range') instead of query('#slider-range'). Basically, js.context allows to access any Javascript variable. By using js.context.$ you get access to the jQuery javascript object (ie. $) on dart side.
import 'dart:html';
import 'package:js/js.dart' as js;
void main() {
  js.scoped(() {
    var slider = js.context.$('#slider-range');
    var options = js.map({
      'range': true,
      'values': [ 17, 67 ]
    });
    slider.slider(options);
  });
}
                        Another sample code:
import 'package:js/js.dart' as js;
js.context.jQuery();
var context = js.context;  
var param = js.map({ 'modal': true, "width":1000, "height":600});
js.context.jQuery("#dialog").dialog(param); 
in html
<script src="packages/browser/interop.js"></script>
The above code open a div as dialog using jQuery.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With