(单单这个标题应该会导致人们走出木制工具来打扰我与俱乐部,但听我说)。GWT中的同步RPC调用
我有一个用例,我需要从异步调用中返回一个值。 (我正在使用GWT平台,但概念是相同的。)我声明了一个最终的JavaScriptObject数组,然后在AsyncCallback中分配值。但是,我需要返回该值,并且该方法在AsyncCallback完成之前返回。因此,我需要阻止,直到AsyncCallback完成。我需要另一种方法返回的值,或者我只需要在onSuccess()中做我需要的。
我试过循环,定时器和其他一些没有运气的方法。谁能帮忙?
@Override
public JavaScriptObject doGetWhereAmIMarker(final double lng, final double lat) {
final JavaScriptObject[] markerArray = new JavaScriptObject[1]; // ugly hack, I know
dispatch.execute(new GetLocationDescriptionsAction(lng, lat), new AsyncCallback<GetLocationDescriptionsResult>() {
@Override
public void onFailure(Throwable caught) {
caught.printStackTrace();
}
@Override
public void onSuccess(GetLocationDescriptionsResult result) {
Map<String, Location> resultMap = result.getResult();
StringBuffer message = new StringBuffer();
for (String key : resultMap.keySet()) {
message.append(key).append(": ").append(resultMap.get(key)).append("\n");
}
Map tempMap = new HashMap();
tempMap.put("TITLE","Location Information");
tempMap.put("LAT", lat);
tempMap.put("LNG", lng);
tempMap.put("CONTENTS", message.toString());
JavaScriptObject marker = GoogleMapUtil.createMarker(tempMap);
markerArray[0] = marker;
if (markerArray[0] != null) {
GWT.log("Marker Array Updated");
}
}
});
return markerArray[0];
}
更新:根据要求,这里是调用doGetWhereIAmMarker()的代码。我尝试过使用Google Map对象(作为JavaScriptObject)作为参数的独立本地方法,但似乎在本地方法之间传递该对象会导致更新所述对象。
public native void initMap(JavaScriptObject mapOptions, JavaScriptObject bounds, JavaScriptObject border, JsArray markerArray, Element e) /*-{
// create the map and fit it within the given bounds
map = new $wnd.google.maps.Map(e, mapOptions);
if (bounds != null) {
map.fitBounds(bounds);
}
// set the polygon for the borders
if (border != null) {
border.setMap(map);
}
// set up the info windows
if (markerArray != null && markerArray.length > 0) {
var infoWindow = new $wnd.google.maps.InfoWindow({
content:"InfoWindow Content Goes Here"
});
for (var i = 0; i < markerArray.length; i++) {
var marker = markerArray[i];
marker.setMap(map);
$wnd.google.maps.event.addListener(marker, 'click', function() {
infoWindow.setContent(marker.content);
infoWindow.open(map, this);
});
}
}
// need to reference the calling class inside the function(), so set a reference to "this"
var that = this;
$wnd.whereAmI=function(lng, lat) {
[email protected]::whereAmI(DD)(lng,lat);
}
$wnd.google.maps.event.addListener(map, 'click', function(event) {
var lat = event.latLng.lat();
var lng = event.latLng.lng();
$wnd.whereAmI(lng, lat);
});
}-*/;
你能展示一些更多的代码吗?我对调用此代码的部分以及使用从'doGetWhereAmIMarker(...)'返回的JavaScriptObject的部分感兴趣。 –
添加了调用本机方法。 GoogleMapUtil位于https://github.com/dartmanx/mapmaker/blob/master/src/main/java/org/jason/mapmaker/client/util/GoogleMapUtil.java,但未提交createMarker()方法。但是,它与createMarkerArray()方法类似。 – Jason
为什么它不起作用,然后如果你只是把一段时间(asyncIsDone){睡眠}。这似乎是最简单的事情......或者让它成为一个for(){sleep},在一段时间过去后它会继续? – Rohan