GWT Synchronous call

John Donvan picture John Donvan · Jun 23, 2012 · Viewed 7.5k times · Source

I have a method in GWT which retrieves the DATA from the DB using the fire method of the requests as you all know its asynchronous I am calling this method from JS so I need to make synchronous is it possible

private static String retriveLocation(String part)
{
    ClientFactory clientFactory = GWT.create(ClientFactory.class);
    MyRequestFactory requestFactory = clientFactory.getRequestFactory();
    YadgetRequest request = requestFactory.yadgetRequest();
    String criteria = "!" + part;
    final ArrayList<String> tags = new ArrayList<String>();

    request.getTagsStartingWith(criteria, 10, 0).fire(
            new Receiver<List<TagProxy>>() {
                @Override
                public void onSuccess(List<TagProxy> tagList) {
                    String output = "[";

                    for (TagProxy pt : tagList) {
                        output += "{";
                        output += "\"id\":" + "\"" + pt.getId() + "\",";
                        output += "\"value\":"
                                + "\""
                                + pt.getName().replaceAll("\"", "")
                                        .replaceAll("!", "") + "\"";
                        output += "},";

                    }
                    if (output.length() > 2)
                        output = output.substring(0, output.length() - 1);
                    output += "]";
                    tags.add(output);

                }

                @Override
                public void onFailure(ServerFailure error) {

                }

            });

    return tags.size() + "";

}

and calling this function from JS like this:

public static native void exportStaticMethod() /*-{
    $wnd.computeLoanInterest =
    $wnd.getAutocomplete [email protected]::retriveLocation(Ljava/lang/String;);
}-*/;

and inside onModuleLoad() I call exportStaticMethod().

and in html I have a button I call onclick getAutocomplete() like this:

<input type="button" onclick="alert(getAutocomplete('j'))" value="momo" /> 

The problem is that the size always returns 0 because the method is asynchronous but if I could return the value onSuccess that would solve my problem. Any ideas please? I have been googling it for 2 days and got no answer.

In other words:

I have JS method I need it to call java method to retrieve data from DB but synchronously!

Example

If I have an HTML button and on click I will pass ID to a function and I need to retrive the name from the DB via GWT and alert it; simply because GWT is asyncronous, I wont be able to do so everytime and when I alert the result, it will be an empty because it's not filled yet.

Answer

AhHatem picture AhHatem · Jun 23, 2012

You cannot use the native GWT RPC synchronously. I am not sure that this is what you are asking, but here is how to make a call to the server synchronously:

private native String makeSyncAjaxCall(String url, String msgText, String conType)/*-{
    var xhReq = new XMLHttpRequest();
    xhReq.open(conType, url, false);
    if(conType == "POST") xhReq.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
    xhReq.send(msgText);
    var serverResponse = xhReq.status + xhReq.responseText;
    return serverResponse;
}-*/; 

Please note that I am not discussing whether that is good idea or not. You should probably stick with Async and put the alert on the success event.