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 =@com.yadget.client.Yadget::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.
From https://stackoverflow.com/a/40610733/6017801:
GWT calls XMLHttpRequest.open() whith true as its third parameter which means the call will be asynchronous. I solved a similar need for testing purposes just forcing this third parameter to be always false:
After invoking fakeXMLHttpRequestOpen(), any further use of XMLHttpRequest will act synchronously. For instance:
will allways render:
See https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/open for XMLHttpRequest.open() specification.
GWT doesn't allow to make synchronous calls to the server, as they can make your browser hang until it gets the response, so for obvious reasons it's best if you can change the flow such that the result from the server will be processed inside the
onSuccess
event handler.You can get data from a database only through java (or using any other server-side language), but not using JavaScript. Just make an asynchronous call from GWT using either
RequestBuilder
or the GWT RPC mechanism. The returned data can than be process inside the appropriate handlers, as mentioned.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:
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.
Ahmad, referring the example you have quoted :
Call a function on Click , say 'getNamefromID()' in GWT , which makes an asynchronous call to the DB and in the onSuccess() function , call a function that will alert the Name.