GWT 中必需的解绑演示者 [英] Unbinding presenters necessary in GWT

查看:13
本文介绍了GWT 中必需的解绑演示者的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我按照此处给出的示例使用 GWT 应用程序中的 MVP 模式 http://code.google.com/webtoolkit/doc/latest/tutorial/mvp-architecture.html

I'm using the MVP pattern from my GWT application following the example given here http://code.google.com/webtoolkit/doc/latest/tutorial/mvp-architecture.html

对于 MainView 中的每个面板,我都有一个 MainPresenter 和 sub-presenter.为了显示一个新的副演示者,我做这样的事情:

I have a single MainPresenter and sub-presenter for each of the panels in the MainView. To show a new sub-presenter, I do something like this:

presenter = new PresenterA(new ViewA(), ....);
presenter.go(panel) // presenter clears the panel and itself to the panel

PresenterA 被创建时,它会将自己绑定到 ViewA 中的事件.我的问题是切换到新演示者的正确方法是什么?现在,我只是创建一个新的演示者并将其附加到同一个面板,如下所示:

When PresenterA is created, it binds itself to events in the ViewA. My question is what is the proper way to switch to a new presenter? Right now, I'm simply creating a new presenter and attaching it to the same panel like this:

presenter = new PresenterB(new ViewB(), ....);
presenter.go(panel) // presenter clears the panel and itself to the panel

我对这种方法有些怀疑.首先,当我切换到新的演示者时是否会导致内存泄漏?我已经丢失了引用旧演示者的字段并将其从它所附加的面板中清除.我想这意味着它应该被垃圾收集,但我不确定.其次,旧演示者拥有的事件绑定会发生什么?这些绑定会阻止演示者被垃圾收集吗?我需要先解除绑定吗?

I have some doubts about this approach. First, am I causing a memory leak when I switch to the new presenter? I've lost both the field that references the old presenter and cleared it from the panel it was attached to. I suppose this means it should be garbage collected, but I'm not really sure. Secondly, what happens to the event bindings that the old presenter had? Will these bindings prevent the presenter from being garbage collected? Do I need unbind them first?

在没有内存泄漏和绑定到死"事件的情况下处理切换演示者情况的正确方法是什么.

What is the correct way the handle the situation of switching presenters without memory leaks and binding to "dead" events.

推荐答案

我建议你看看 gwt-mvp 和/或 gwt-presenter 库,它们都采用相同的方法来解决这个问题.实际上,您为所有演示者创建了一个基类,该基类维护演示者拥有的所有事件注册的内部列表.当您切换演示者时,您对旧演示者调用 presenter.unbind(),然后删除您创建的所有事件处理程序.

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);
    }

}

然后在演示者的绑定方法中,将 HandlerRegistration 对象传递给 addHandler() 方法:

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

bind() {
    addHandler(foo.addBarHandler(...));
}

这篇关于GWT 中必需的解绑演示者的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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