解除GWT中必需的主持人 [英] Unbinding presenters necessary in GWT

查看:110
本文介绍了解除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

我有一本MainPresenter和MainView中每个面板的子演示者。为了展示新的子演示者,我做了这样的事情:

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?

处理切换演示者的情况的正确方法是什么?没有内存泄漏并绑定到死事件。 b $ b

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天全站免登陆