GWT同步呼叫 [英] GWT Synchronous call

查看:171
本文介绍了GWT同步呼叫的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个GWT中的方法,它使用请求的fire方法从数据库中检索数据,因为你们都知道它的异步方式,我从JS调用这个方法,所以我需要使它成为同步的

  private static String retriveLocation(String part)
{
ClientFactory clientFactory = GWT.create( ClientFactory.class);
MyRequestFactory requestFactory = clientFactory.getRequestFactory();
YadgetRequest request = requestFactory.yadgetRequest();
String criteria =! +部分;
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 + ={;
输出+ =\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){


$ b $} ;

返回tags.size()+;


$ / code>

并且像这样从JS中调用这个函数:

  public static native export exportStaticMethod()/ *  -  {
$ wnd.computeLoanInterest =
$ wnd.getAutocomplete =@com.yadget.client.Yadget :: retriveLocation(Ljava / lang / String;);
} - * /;

和里面 onModuleLoad() code> exportStaticMethod()



和html我有一个按钮,我调用onclick getAutocomplete() >像这样:


$ b

 < input type =buttononclick =alert(getAutocomplete('j'))value =momo/> 

问题是大小始终返回0,因为该方法是异步的,但如果我可以返回值 onSuccess 这将解决我的问题。请有任何想法吗?我已经使用Google搜索了2天并没有得到任何答案。换句话说:

我有JS方法我需要它调用java方法来从数据库检索数据,但同步!

示例

如果我有一个HTML按钮,点击后我会将ID传递给一个函数,通过GWT从DB中检索名称并提醒它;只是因为GWT是异步的,我不能每次都这样做,当我提醒结果时,它将是一个空的,因为它还没有被填充。

解决方案

不能同步使用本地GWT RPC。
我不确定这是你所问的,但是这里是如何同步调用服务器:

  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;
返回serverResponse;
} - * /;

请注意,我并不是在讨论这是不是个好主意。您应该坚持使用Async并将警报放在成功事件中。


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.

解决方案

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.

这篇关于GWT同步呼叫的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆