Calling Java Method from GWT JSNI
First, yes I have searched already and found this answer already:
GWT JSNI - problem passing Strings
I'm trying to call a java method from a JSNI method, but not getting anywhere. I've tried the advice given above, but it still won't work.
Here's the code:
public native void initCustomListeners(MapmakerMapViewPresenter.MyView view) /*-{
//public native void initCustomListeners() /*-{
$wnd.getLocationDescriptions = function(lng, lat) {
开发者_高级运维 $entry(view.@org.jason.mapmaker.client.view.MapmakerMapViewImpl::getLocationDescriptions(DD)(lng, lat));
}
$wnd.google.maps.event.addListener(map, 'click', function(event) {
var lat = event.latLng.lat().toFixed(3);
var lng = event.latLng.lng().toFixed(3);
alert("(" + lat + ", " + lng + ")");
$wnd.getLocationDescriptions(lng, lat);
alert("Test!");
});
}-*/; @Override
public void getLocationDescriptions(double lng, double lat) {
getUiHandlers().doGetLocationDescriptions(lng, lat);
}
Can anyone help me out?
Jason
I don't know if that's the issue (you don't even say how that code behaves vs. how you expect it to behave) but there are a few bugs in your code:
$entry
wraps a function, so you have to call the function it returns, rather than (uselessly) wrap the result of the function after calling it! I.e.$entry(function(lat,lng) { foo.@bar.Baz::quux(DD)(a, b); }
rather than$entry(foo.@bar.Baz::quux(DD)(a, b))
you
addListener
on amap
but that variable is never defined.
I'm still missing something, and I've stared at the given code for quite awhile.
Here's the current version of the code:
public native void initCustomListeners(JavaScriptObject map) /*-{
var that = this;
$wnd.getLocationDescriptions = function(lng, lat) {
$entry(that.@org.jason.mapmaker.client.presenter.MapmakerMapViewPresenter::doGetLocationDescriptions(DD))(lng,lat);
}
$wnd.google.maps.event.addListener(map, 'click', function(event) {
var lat = event.latLng.lat();
var lng = event.latLng.lng();
alert("(" + lat + ", " + lng + ")");
@com.google.gwt.core.client.GWT::log(Ljava/lang/String;)("calling $wnd.getLocationDescriptions()");
$wnd.getLocationDescriptions(lng, lat);
@com.google.gwt.core.client.GWT::log(Ljava/lang/String;)("called $wnd.getLocationDescriptions()");
});
}-*/;
The call is resulting in a ClassCastException. Sorry if I've missed something that should be obvious.
One mistake I can see is that you should do:
$wnd.getLocationDescriptions = $entry(@org.jason.mapmaker.client.view.MapmakerMapViewImpl::getLocationDescriptions(DD)(lng, lat));
(not with the function 'wrapper' you use), and then call the function from javascript via:
$wnd.getLocationDescriptions(lng, lat);
Also, the 'that' variable is not necessary (or 'this'), before the @. I'm also not sure about the $wnd.google.maps.event.addListener( thing, are you sure there's such an object assigned on $wnd?
Last, take one more look at this:
https://developers.google.com/web-toolkit/doc/latest/DevGuideCodingBasicsJSNI
精彩评论