Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dart js-interop with jquery ui

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');
  });
}
like image 836
0tto Avatar asked Nov 02 '12 13:11

0tto


2 Answers

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);
  });
}
like image 198
Alexandre Ardhuin Avatar answered Sep 23 '22 00:09

Alexandre Ardhuin


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.

like image 24
greensuisse Avatar answered Sep 21 '22 00:09

greensuisse