异步开始和结束时的Resty-GWT自定义回调 [英] Resty-GWT custom callback on async start and end

查看:163
本文介绍了异步开始和结束时的Resty-GWT自定义回调的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将resty gwt用于所有服务器通信。我希望有一些指标可以表明该操作正在进行中。

I use resty gwt for all server communication. I would like some indicator that would show the operation is in progress.

我考虑了2个方法:


  • 进度条,将显示进度百分比;

  • 动画,将在操作进行中显示,但没有任何提示。

我假设我需要添加带有回调的自定义过滤器。
我想触发以下事件: RestyGwtComunicationStart RestyGwtComunicationEnd ,或回调触发 onCommunicationStarted
。我想在RestyGWT Dispatcher配置的一个地方声明它。另外,如果有错误,我想提取该错误。

I've assumed that I need to add custom filter with callback. I would like to fire events like: RestyGwtComunicationStart and RestyGwtComunicationEnd, or callback to fire onComunicationStarted and onComunicationEnded. I would like to have this declared in one place, RestyGWT Dispatcher configuration. Also if there was an error I would like to fetch the error.

但是我不知道从哪里开始。在文档中对此一无所知。

But I don't know where to start. There is no word about it in documentations.

我可以向您寻求帮助吗?我该怎么办?

Can I ask You for help? How can I do this?

推荐答案

不幸的是,我没有得到足够的答案,所以我开发了自己的解决方案。

Unfortunately I did not get adequate answer, So I developed my own solution.

首先,我已将Resty配置 RestyGwtConfig 添加到我的模块配置中

At first I've added Resty configuration RestyGwtConfig to my Module configuration

public class ClientModule extends AbstractPresenterModule {
    @Override
    protected void configure() {
        bind(RestyGwtConfig.class).asEagerSingleton();
        install(new DefaultModule.Builder()
        .defaultPlace(Routing.HOME.url)
        .errorPlace(Routing.ERROR.url)
        .unauthorizedPlace(Routing.LOGIN.url)
        .tokenFormatter(RouteTokenFormatter.class).build());
        install(new AppModule());
        install(new GinFactoryModuleBuilder().build(AssistedInjectionFactory.class));
        bind(ResourceLoader.class).asEagerSingleton();
    }
}

然后我为所有通信设置了自定义分配器

then I've set Custom distpatcher for all my comunication requests of resty gwt.

import org.fusesource.restygwt.client.Defaults;
import org.fusesource.restygwt.client.Resource;
import pl.korbeldaniel.cms.shared.ServiceRouting;
import com.google.gwt.core.client.GWT;
import com.google.inject.Inject;

public class RestyGwtConfig {
    @Inject
    public RestyGwtConfig(RestyDispatcher dispatcher) {
        Defaults.setDispatcher(dispatcher);
    }
}

然后我添加了自定义过滤器( ProgressIndicatorFilter )来处理通信的开始结束回调

Then I've added custom filter (ProgressIndicatorFilter) to handle communication's start and end callbacks:

import org.fusesource.restygwt.client.Method;
import org.fusesource.restygwt.client.dispatcher.DefaultFilterawareDispatcher;
import com.google.gwt.http.client.Request;
import com.google.gwt.http.client.RequestBuilder;
import com.google.gwt.http.client.RequestException;
import com.google.inject.Inject;

public class RestyDispatcher extends DefaultFilterawareDispatcher {
    @Inject
    public RestyDispatcher(ProgressIndicatorFilter progressIndicatorFilter) {
        addFilter(progressIndicatorFilter);
    }
}

在过滤器类方法中被覆盖过滤器我添加了一个事件触发器( eventBus.fireEvent(new IndicatorEvent( Rest-Gwt通信已开始)); )并注册了回调,这是完整的代码:

in filter class method overriden filter I've added an event trigger (eventBus.fireEvent(new IndicatorEvent("Rest-Gwt Comunication started"));) and registered callback, here is whole code:

import org.fusesource.restygwt.client.Method;
import org.fusesource.restygwt.client.dispatcher.DispatcherFilter;
import pl.korbeldaniel.cms.client.template.progressIndicator.IndicatorEvent;
import com.google.gwt.http.client.RequestBuilder;
import com.google.inject.Inject;
import com.google.web.bindery.event.shared.EventBus;

class ProgressIndicatorFilter implements DispatcherFilter {
    private AssistedInjectionFactory factory;
    private EventBus eventBus;

    @Inject
    public ProgressIndicatorFilter(AssistedInjectionFactory factory, EventBus eventBus) {
        this.factory = factory;
        this.eventBus = eventBus;
    }
    @Override
    public boolean filter(Method method, RequestBuilder builder) {
        builder.setCallback(factory.createProgressIndicatorCallback(method));
        eventBus.fireEvent(new IndicatorEvent("Resty-Gwt Comunication started"));
        return true;
    }
}

无法直接注册回调,像

new ProgressIndicatorDispatcherCallback()

使用依赖注入。因此,我创建了一个工厂来辅助注入,如下所示:

cause I use dependency injection. So I've created a factory to assist injection as follow:

public interface AssistedInjectionFactory {
    ProgressIndicatorDispatcherCallback createProgressIndicatorCallback(Method method);
}

此处此处,您可以找到更多辅助注射信息。

Here and here You can find more Assisted Injection info.

这是回调代码:

class ProgressIndicatorDispatcherCallback implements RequestCallback {
    private RequestCallback requestCallback;
    private EventBus eventBus;

    @Inject
    public ProgressIndicatorDispatcherCallback(@Assisted Method method, EventBus eventBus) {
        this.requestCallback = method.builder.getCallback();
        this.eventBus = eventBus;
    }
    @Override
    public void onResponseReceived(Request request, Response response) {
        endComunicationFireIvent();
        requestCallback.onResponseReceived(request, response);
    }
    @Override
    public void onError(Request request, Throwable exception) {
        endComunicationFireIvent();
        requestCallback.onError(request, exception);
    }
    private void endComunicationFireIvent() {
        eventBus.fireEvent(new IndicatorEvent("Rest-Gwt Comunication ended"));
    }
}

这篇关于异步开始和结束时的Resty-GWT自定义回调的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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