Google japi doesn't appear to play nice with js interop

79 views
Skip to first unread message

Cogman

unread,
Apr 1, 2014, 9:10:03 PM4/1/14
to mi...@dartlang.org
Simple code, am I doing it wrong?

My html looks like this (using dart 1.3.0-dev.61 and js 0.2.2)

    <script type='text/javascript' src='https://www.google.com/jsapi'></script>
    <script type="application/dart" src="statsstuff.dart"></script>
    <script src="packages/browser/dart.js"></script>

I call the load functionality like so

  js.context.google.load('jquery', '1');
  js.context.google.setOnLoadCallback(doThings);

and in 'doThings'

  js.context.$('#bob');

blows chunks

Is this a bug or am I doing things wrong?  I can move the load method into native javascript and everything works fine, it is just in dart code that things appear to break down.

Alexandre Ardhuin

unread,
Apr 2, 2014, 2:32:18 AM4/2/14
to General Dart Discussion
What is the error you get ?

Alexandre


--
For other discussions, see https://groups.google.com/a/dartlang.org/
 
For HOWTO questions, visit http://stackoverflow.com/tags/dart
 
To file a bug report or feature request, go to http://www.dartbug.com/new

To unsubscribe from this group and stop receiving emails from it, send an email to misc+uns...@dartlang.org.

Cogman

unread,
Apr 2, 2014, 7:27:16 AM4/2/14
to mi...@dartlang.org

The error is that nothing happens. The script never loads when load is called. No exception is returned from the load line. Instead, the exception happens when you try to do anything with jquery

Cogman

unread,
Apr 2, 2014, 9:12:48 AM4/2/14
to mi...@dartlang.org
Here is a more complete example of it. (following docs here https://developers.google.com/loader/ )

The code

import 'package:js/js.dart' as js;

void main() {
  js.context.google.load('jquery', '1');
  js.context.google.setOnLoadCallback(onLoad);
}

void onLoad(var e){
  js.context.$('#jqueryTest').text('bob');
}

The Html

<!DOCTYPE html>

<html>
  <body>
    <div id="jqueryTest"></div>
    
    <script type="text/javascript" src="https://www.google.com/jsapi"></script>
    <script type="application/dart" src="jsapitest.dart"></script>
    <script src="packages/browser/dart.js"></script>
  </body>
</html>

In dartium, the output is nothing.  There is no indication that anything went wrong.  In Javascript we get this

Uncaught TypeError: Cannot call method 'apply' of undefined

However, if I add this to the html following all the other script tags

    <script type="text/javascript">
      google.load('jquery', '1');
    </script>

Everything works as expected.

Alexandre Ardhuin

unread,
Apr 2, 2014, 9:36:01 AM4/2/14
to General Dart Discussion, Justin Fagnani
+Justin

The behaviour of google Loader is really strange !!!!

In the following example if I call 'google.load' from Dart, I can see jQuery loaded in Network but the callback is not called. However if I call 'google.load' from JS jQuery is also loaded but now the callback is called. I really don't understand what is going on there :/

---- HTML
    <!DOCTYPE html>

    <script type='text/javascript' src='https://www.google.com/jsapi'></script>
    <script type='text/javascript'>
    //google.load('jquery', '1');
    </script>
    <script type="application/dart" src="js.dart"></script>
    <script src="packages/browser/dart.js"></script>

----- DART
    import 'dart:js' as js;
    main() {
      js.context['google'].callMethod('load',['jquery', '1']);
      js.context['google'].callMethod('setOnLoadCallback',[() => print('dart')]);
    }

@Justin : if you have an idea ?

Alexandre

Alexandre Ardhuin

unread,
Apr 2, 2014, 9:38:05 AM4/2/14
to General Dart Discussion, Justin Fagnani
Ooops, here's the good Dart file ( the callback need one parameter )

----- DART
    import 'dart:js' as js;
    main() {
      js.context['google'].callMethod('load',['jquery', '1']);
      js.context['google'].callMethod('setOnLoadCallback',[(_) => print('dart')]);
    }
Alexandre

Cogman

unread,
Apr 2, 2014, 9:56:58 AM4/2/14
to mi...@dartlang.org

Justin Fagnani

unread,
Apr 2, 2014, 8:20:44 PM4/2/14
to General Dart Discussion
I don't have any ideas off the top of my head. I'll take a look at this soon.

Cogman

unread,
Apr 3, 2014, 12:58:34 PM4/3/14
to mi...@dartlang.org
Ok, Thanks for looking into this.
Reply all
Reply to author
Forward
0 new messages