Unbinding presenters necessary in GWT

大憨熊 提交于 2019-12-01 05:21:42

I'd suggest that you take a look at the gwt-mvp and/or gwt-presenter libraries, which both take the same approach to this problem. Effectively, you create a base class for all presenters which maintains an internal list of all event registrations that the presenter has. When you then come to switch presenters, you call presenter.unbind() on the old presenter, which then removes all the event handlers you've created.

The base presenter class will look something like this:

public abstract class BasePresenter {

    private List<HandlerRegistration> registrations = Lists.newLinkedList();

    public void bind() {}

    public void unbind() {
        for(HandlerRegistration registration : registrations) {
            registration.removeHandler();
        }
        registrations.clear();
    }

    protected void addHandler(HandlerRegistration registration) {
        registrations.add(registration);
    }

}

Then in the bind method of your presenter, you pass the HandlerRegistration object's into the addHandler() method:

bind() {
    addHandler(foo.addBarHandler(...));
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!