9

This is a typical situation in jQuery:

$(".myClass").myFunction({
    aKey: 'some value'
});

How do you call that using dart:js?

The documentation is a bit cryptic and a similar question that I found here seems dated.

Community
  • 1
  • 1
alearg
  • 685
  • 7
  • 17

3 Answers3

10

You can do :

main() {
  js.context.callMethod(r'$', ['.myClass'])
      .callMethod('myFunction', [new js.JsObject.jsify({'aKey': 'some value'})]);
}
Alexandre Ardhuin
  • 71,959
  • 15
  • 151
  • 132
  • 1
    Yikes. I don't suppose there's any reason to actually do this, right? Or will I be having to maintain code like this in another few years... (I mean, there's probably no advantage to using dart in a situation like this, so we probably won't see much of this kind of thing, right?) – Dagg Nabbit Dec 24 '13 at 06:03
  • 1
    Actually _package:js_ provides a simpler API and you can keep on using it. See the similar question you mention. See also http://stackoverflow.com/questions/20590148/how-do-you-interact-with-js-from-dart/20594873#20594873 for _dart:js_ vs. _package:js_. – Alexandre Ardhuin Dec 24 '13 at 06:14
  • Truly acrobatic! Thanks a lot. – alearg Dec 24 '13 at 17:35
4

You can use the build-in funcitons querySelector or querySelectorAll instead of the jQuery selector. So it would be:

main(){   
    querySelector(".myClass").myFunction(){
        aKey: 'some value'
    } 
}

or for mulitple elements:

main(){
    querySelectorAll(".myClass").myFunction(){
        aKey: 'some value'
    } 
}
XD face me
  • 578
  • 1
  • 3
  • 12
0

How about use DQuery instead.

DQuery is a porting of jQuery in Dart.

wangkaibule
  • 808
  • 1
  • 9
  • 20