If I understand correctly, you want to call a GWT method from a plain,
non-GWT "A" tag. To do this there are two things you need to know:
1) the compiled GWT script runs in an embedded iframe in the host
page, therefore GWT runs in different scope than JavaScript in host
page. You *can* access external JavaScript entities from GWT via the
$wnd variable. However, you cannot (AFAIK) access the embedded GWT
script from external scripts (read on for workaround).
2) the GWT script is obfuscated. Even if you could call into the GWT
script, you would find that the method name has been changed. GWT does
not provide a way to selectively disable obfuscation. You could
compile with the -PRETTY option to prevent the entire script from
being obfuscated, but this results in a considerably larger download.
The solution:
GWT can access the external JavaScript scope via $wnd in a JSNI
method. You can define a function on $wnd which calls your GWT method.
Your GWT method name will be obfuscated in the compiled script, but
this will not be a problem: the GWTCompiler parses your JSNI code and
updates references to obfuscated methods.
You can see this technique in action in the GWT Tk Debug demo. Click
on the "regular JavaScript" link.
http://www.asquare.net/gwttk/apps/demo/Demo.html#debug
HTML source:
<a href='#' onclick="Debug.println('This is a pure HTML onclick
handler');return false;">regular JavaScript</a>
GWT Source:
http://gwt-tk.googlecode.com/svn/tk/trunk/src/java/asquare/gwt/debug/client/Debug.java
--
Mat Gessel
http://www.asquare.net/gwttk/
I tried it and it works fine for static methods. Is there a way to get
this working with non-static methods? E.g. clicking on a html link
calls a nonstatic method that runs an rpc call and updates widgets
afterwards.
> GWT Source:
> http://gwt-tk.googlecode.com/svn/tk/trunk/src/java/asquare/gwt/debug/client/Debug.java
in your jsInit() i found this line: if ($wnd.Debug === undefined)
Is this intentionally?
If the GWT method is not static, then it cannot be known at compile
time. You would need to create a facility with which the GWT object
can let the native object know the GWT object is interested. It could
register in a look up table or set a callback. I'm not sure about
this, but you may have to clean up references to prevent memory leaks
in IE.
You may want to consider putting your content into an HTMLPanel and
using a SimpleHyperLink to trigger the action.
http://gwt-tk.googlecode.com/svn/tk/trunk/src/java/asquare/gwt/tk/client/ui/SimpleHyperLink.java
Another possibility is the Wrapper classes in GWT WL:
http://gwt-widget.sourceforge.net/docs/apidocs/org/gwtwidgets/client/wwrapper/package-summary.html
> > GWT Source:
> > http://gwt-tk.googlecode.com/svn/tk/trunk/src/java/asquare/gwt/debug/client/Debug.java
>
> in your jsInit() i found this line: if ($wnd.Debug === undefined)
> Is this intentionally?
Yes. (typeof $wnd.Debug == "undefined") would probably work too,
though the expression in question seems cleaner to me.
the problem is, I am using dynamically generated image maps. I think
its less complicated to use a HTML Panel, fill it with image and map
tags and use document.getElementById('id').click() as hook back to gwt.
<area shape="rect"
href="javascript:document.getElementById('gwtButton').click();" ...
where gwtButton is the id of a GWT Button Widget that has a
eventlistener which fires the rpc