Hi,

It might be the case that in both cases your GWT code is loaded and
executing before the browser has loaded your externally referenced
JavaScript file.  To remove that possiblity, you can place the
<script> tag you have in the HTML into your module's .gwt.xml
definition, i.e.

<module>
   <inherits name='com.google.gwt.user.User' />
   <script type="text/javascript" src="protovis.js">
</module>

With this sett up, the GWT boostrap code should ensure the library
JavaScript is loaded before your GWT code executes.

//Adam

On 19 Juli, 18:19, wsaleem <[email protected]> wrote:
> I am not a JS developer and use it pretty much by example, so it might
> be that I am missing something really basic below.
>
> I have come across problems using JSNI in the following 2 scenarios:
>
> 1.
> I use Google Visualization API successfully in JS as follows:
> ********************
> HTML file
>     <script type="text/javascript" src="http://www.google.com/jsapi";></
> script>
> JS file
>     google.load('visualization', '1', {'packages':['piechart']});  //
> <== future JSNI problems occur here
>     var chart = new google.visualization.LineChart
> (document.getElementById('GoogleVisChart'));
>     // other JS commands to draw the chart
> **********
> and then try to write a wrapper using GWT JSNI as follows:
> ********************
> HTML file
>     <script type="text/javascript" src="http://www.google.com/jsapi";></
> script>
> JAVA file
>     class GChart {
>       static { _init(); }
>       private static native void _init() /*-{ // <=== ERROR at this
> function
>         $wnd.google.load('visualization', '1', {'packages':
> ['piechart']});
>       }-*/;
>
>       JavaScriptObject gvis;
>       public GChart( String id ) { gvis = _chart( id ); }
>       private native JavaScriptObject _chart( String id ) /*-{
>         return new $wnd.google.visualization.LineChart
> ($wnd.document.getElementById( id ) );
>       }-*/;
>       // methods to draw the chart through native functions
> **********
> The GWT compiler gives the following error for the above code
> ********************
> [ERROR] Unable to load module entry point class
> com.google.gwt.app.testGViz.client.TestGViz (see associated exception
> for details)
> java.lang.RuntimeException: Failed to invoke native method:
> @com.google.gwt.app.testGViz.client.GChart::_init() with 0 arguments.
>         at com.google.gwt.dev.shell.moz.LowLevelMoz.invoke(LowLevelMoz.java:
> 132)
>         at com.google.gwt.dev.shell.moz.ModuleSpaceMoz.doInvoke
> (ModuleSpaceMoz.java:98)
>         at com.google.gwt.dev.shell.ModuleSpace.invokeNative(ModuleSpace.java:
> 447)
>         at com.google.gwt.dev.shell.ModuleSpace.invokeNativeVoid
> (ModuleSpace.java:248)
>         at com.google.gwt.dev.shell.JavaScriptHost.invokeNativeVoid
> (JavaScriptHost.java:107)
>         at com.google.gwt.app.testGViz.client.GChart._init(GChart.java)
>         at com.google.gwt.app.testGViz.client.GChart.<clinit>(GChart.java:7)
>         at com.google.gwt.app.testGViz.client.TestGViz.onModuleLoad
> (TestGViz.java:21)
>         at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
>         at sun.reflect.NativeMethodAccessorImpl.invoke
> (NativeMethodAccessorImpl.java:39)
>         at sun.reflect.DelegatingMethodAccessorImpl.invoke
> (DelegatingMethodAccessorImpl.java:25)
>         at java.lang.reflect.Method.invoke(Method.java:597)
>         at com.google.gwt.dev.shell.ModuleSpace.onLoad(ModuleSpace.java:320)
>         at com.google.gwt.dev.shell.BrowserWidget.attachModuleSpace
> (BrowserWidget.java:329)
>         at com.google.gwt.dev.shell.moz.BrowserWidgetMoz.access$100
> (BrowserWidgetMoz.java:35)
>         at com.google.gwt.dev.shell.moz.BrowserWidgetMoz
> $ExternalObjectImpl.gwtOnLoad(BrowserWidgetMoz.java:59)
>         at org.eclipse.swt.internal.gtk.OS._g_main_context_iteration(Native
> Method)
>         at org.eclipse.swt.internal.gtk.OS.g_main_context_iteration(OS.java:
> 1428)
>         at org.eclipse.swt.widgets.Display.readAndDispatch(Display.java:2840)
>         at com.google.gwt.dev.GWTShell.pumpEventLoop(GWTShell.java:720)
>         at com.google.gwt.dev.GWTShell.run(GWTShell.java:593)
>         at com.google.gwt.dev.GWTShell.main(GWTShell.java:357)
> **********
> Moving the statement in the native _init function to the constructor
> does not help either. To get it to work, I have to move it out to the
> HTML file
> ********************
> HTML file
>     <script type="text/javascript" src="http://www.google.com/jsapi";></
> script>
>     <script type="text/javascript">
>         google.load('visualization', '1', {'packages':['piechart']});
>     </script>
> **********
> This was also suggested 
> inhttp://groups.google.com/group/Google-Web-Toolkit/browse_thread/threa...
> Although this works, I do not understand why I could no do the
> google.load through JSNI.
> In the meanwhile, this is not an issue as GWT now provides its own
> wrappers for the Google Visualization API. But, I would still like to
> understand the error.
>
> 2.
> I am trying this time to write a wrapper for another Visualization
> library, Protovis. An example JS use case is as follows:
> ********************
> HTML file
>     <script type="text/javascript" src="protovis.js"></script>
> JS file
>     var vis = new pv.Panel().canvas("PVis"); // <== future JSNI
> problems occur at this point
>     var line = vis.add(pv.Line);
>     // add data and attributes to line
>     vis.render();
> **********
> I write a GWT wrapper as follows
> ********************
> JAVA file
>     class PChart {
>       JavaScriptObject pvis;
>       public PChart( String id ) { pvis = _chart( id ); }
>       private native JavaScriptObject _chart( String id ) /*-{
>         return new $wnd.pv.Panel().canvas( id );  // <=== ERROR at
> this line
>       } -*/;
>       // other stuff
> **********
> The GWT compiler returns the following error at the indicated line
> ********************
> com.google.gwt.core.client.JavaScriptException: (TypeError): $wnd.pv
> has no properties
> **********
>
> I do not understand what I am doing wrong. Can someone help me to get
> this to work?
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Google Web Toolkit" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/Google-Web-Toolkit?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to