9

(仅凭这个标题就应该引起人们从木制品中走出来用棍棒殴打我,但请听我说)。

我有一个用例,我需要从异步调用中返回一个值。(我使用的是 GWT-Platform,但概念是相同的。)我声明了一个最终的 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) {
        that.@org.jason.mapmaker.client.view.MapmakerMapViewImpl::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);
    });

}-*/;
4

2 回答 2

3

在某些时候,我不得不做一些类似的事情,但最终我删除了那些代码以支持异步的东西。因此,我不能给出您需要使用的确切代码,但只能给出一些关于如何处理它的指示。

  • 首先,这篇博客描述了如何使用 javascript 进行同步 AJAX。
  • 其次,您必须为同步调用提供支持。问题是 GWT 不支持提供同步 AJAX 调用的参数。很可能是他们不想鼓励使用它。因此,您需要使用 JSNI 将适当的方法添加到XMLHttpRequest(您可能会扩展),然后再添加到RequestBuilder(也应该扩展它)。
  • 最后,使用扩展修改您的服务RequestBuilder。就像是

((ServiceDefTarget)service).setRpcRequestBuilder(requestBuilder);

总而言之 - 来自同一篇博客文章(略微脱离上下文):

由于请求丢失和挂起浏览器的危险,不建议将同步 javascript 用于 (onbefore)unload 事件处理程序之外的任何内容。

于 2011-09-08T20:02:17.910 回答
0

我认为这一切都是命运......
我们不能在 Gwt 中捕获响应并发送它,因为在请求发送后立即开始执行下一个方法,既不打扰响应,但他们仍然满足我们使用计时器,是我怎么想...

 Timer t = new Timer() {
      @Override
      public void run() {
        Window.alert("Nifty, eh?");
      }
    };
    t.schedule(5000);
于 2013-09-27T10:52:27.900 回答