活动处理程序不会被删除

发布于 2024-12-13 02:37:52 字数 2679 浏览 1 评论 0原文

我正在尝试加快使用 GWT 活动和地点的速度。我正在使用最初在这个好 博客文章

我发现在 bind() 期间添加的处理程序似乎从未被删除。我对 的一点了解Activity javadoc 让我认为它们应该在调用 Activity 的 onStop() 方法时自动删除。

在此之前它注册的所有事件处理程序都将被删除 方法被调用。

但每次我单击一个按钮,相应的处理程序都会被调用 n+1 次。

我缺少什么?如果我可以提供更多信息,请告诉我。

这是代码中的相关片段:

public class ContactsActivity extends AbstractActivity {

private List<ContactDetails> contactDetails;
private final ContactsServiceAsync rpcService;
private final EventBus eventBus;
private final IContactsViewDisplay display;
private PlaceController placeController;

public interface IContactsViewDisplay {
    HasClickHandlers getAddButton();
    HasClickHandlers getDeleteButton();
    HasClickHandlers getList();
    void setData(List<String> data);
    int getClickedRow(ClickEvent event);
    List<Integer> getSelectedRows();
    Widget asWidget();
}

public ContactsActivity(ClientFactory factory) {
    GWT.log("ContactActivity: constructor");

    this.rpcService = factory.getContactServiceRPC();
    this.eventBus = factory.getEventBus();
    this.display = factory.getContactsView();
    this.placeController = factory.getPlaceController();
}

@Override
public void start(AcceptsOneWidget container, EventBus eventBus) {
    GWT.log("ContactActivity: start()");

    bind();
    container.setWidget(display.asWidget());
    fetchContactDetails();

}

public void bind() {

    GWT.log("ContactActivity: bind()");

    display.getAddButton().addClickHandler(new ClickHandler() {
        public void onClick(ClickEvent event) {
            GWT.log("Add button clicked");
            ContactsActivity.this.placeController.goTo(new NewContactPlace(""));
        }
    });

    display.getDeleteButton().addClickHandler(new ClickHandler() {
        public void onClick(ClickEvent event) {
            GWT.log("ContactActivity: Delete button clicked");
            deleteSelectedContacts();
        }
    });

    display.getList().addClickHandler(new ClickHandler() {
        public void onClick(ClickEvent event) {
            GWT.log("ContactActivity: List clicked");
            int selectedRow = display.getClickedRow(event);

            if (selectedRow >= 0) {
                String id = contactDetails.get(selectedRow).getId();
                ContactsActivity.this.placeController.goTo(new EditContactPlace(id));
            }
        }
    });
}

I'm trying to get up to speed on using GWT Activities and Places. I'm testing with some source code originally found on this good blog post.

I'm finding the Handlers that get added during bind() never seem to removed. My little understanding of the Activity javadoc had me thinking they should get automagically removed by the time the Activity's onStop() method is invoked.

All event handlers it registered will have been removed before this
method is called.

But each time I click a button the corresponding handler is called n+1 times.

What am I missing? Please let me know if there is more info I can provide.

Here's a relevant snippet from the code:

public class ContactsActivity extends AbstractActivity {

private List<ContactDetails> contactDetails;
private final ContactsServiceAsync rpcService;
private final EventBus eventBus;
private final IContactsViewDisplay display;
private PlaceController placeController;

public interface IContactsViewDisplay {
    HasClickHandlers getAddButton();
    HasClickHandlers getDeleteButton();
    HasClickHandlers getList();
    void setData(List<String> data);
    int getClickedRow(ClickEvent event);
    List<Integer> getSelectedRows();
    Widget asWidget();
}

public ContactsActivity(ClientFactory factory) {
    GWT.log("ContactActivity: constructor");

    this.rpcService = factory.getContactServiceRPC();
    this.eventBus = factory.getEventBus();
    this.display = factory.getContactsView();
    this.placeController = factory.getPlaceController();
}

@Override
public void start(AcceptsOneWidget container, EventBus eventBus) {
    GWT.log("ContactActivity: start()");

    bind();
    container.setWidget(display.asWidget());
    fetchContactDetails();

}

public void bind() {

    GWT.log("ContactActivity: bind()");

    display.getAddButton().addClickHandler(new ClickHandler() {
        public void onClick(ClickEvent event) {
            GWT.log("Add button clicked");
            ContactsActivity.this.placeController.goTo(new NewContactPlace(""));
        }
    });

    display.getDeleteButton().addClickHandler(new ClickHandler() {
        public void onClick(ClickEvent event) {
            GWT.log("ContactActivity: Delete button clicked");
            deleteSelectedContacts();
        }
    });

    display.getList().addClickHandler(new ClickHandler() {
        public void onClick(ClickEvent event) {
            GWT.log("ContactActivity: List clicked");
            int selectedRow = display.getClickedRow(event);

            if (selectedRow >= 0) {
                String id = contactDetails.get(selectedRow).getId();
                ContactsActivity.this.placeController.goTo(new EditContactPlace(id));
            }
        }
    });
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

还如梦归 2024-12-20 02:37:52

事件通过注册。传递给 AbstractActivity#start()EventBus 将在调用 onStop() 时取消注册。然而,在上述 bind() 方法中注册的事件处理程序不是通过 EventBus 注册的,并且对抽象基类不可见。您需要自己注销它们:

public class ContactsActivity extends AbstractActivity {
  private List<HandlerRegistration> registrations = new ArrayList();

  private void bind() {
    registrations.add(display.getAddButton().
      addClickHandler(new ClickHandler() { ... }));
    registrations.add(display.getDeleteButton().
      addClickHandler(new ClickHandler() { ... }));
    registrations.add(display.getList().
      addClickHandler(new ClickHandler() { ... }));
  }

  @Override
  public void onStop() {
    for (HandlerRegistration registration : registrations) {
      registration.removeHandler();
    }

    registrations.clear();
  }
}

Events registered via. the EventBus passed to AbstractActivity#start() will be unregistered by the time onStop() is called. The event handlers registered in the above bind() method, however, are not registered via the EventBus and are not visible to the abstract base class. You need to unregister them yourself:

public class ContactsActivity extends AbstractActivity {
  private List<HandlerRegistration> registrations = new ArrayList();

  private void bind() {
    registrations.add(display.getAddButton().
      addClickHandler(new ClickHandler() { ... }));
    registrations.add(display.getDeleteButton().
      addClickHandler(new ClickHandler() { ... }));
    registrations.add(display.getList().
      addClickHandler(new ClickHandler() { ... }));
  }

  @Override
  public void onStop() {
    for (HandlerRegistration registration : registrations) {
      registration.removeHandler();
    }

    registrations.clear();
  }
}
温柔一刀 2024-12-20 02:37:52

我发现最好在视图中处理注册 - 使其只负责为每个按钮保持一次单击处理程序处于活动状态。

而不是:

class View {
    Button commitButton;

    public HasClickHandlers getCommit () {return commitButton;}
}

..并在活动中链接到此:

view.getCommit.addClickHandler(new Clickhandler()...

在视图中执行此操作:

    class View {
        private Button commitButton;        
        private HandlerRegistration commitRegistration = null;

        public void setCommitHandler (ClickHandler c) {
            commitRegistraion != null ? commitRegistration.removeRegistration ();
            commitRegistration = commitButton.addClickHandler (c);
        }
    }

和活动:

view.setCommitHandler (new ClickHandler () ...

希望有帮助。

I found it best to handle registration in the view - make it responsible for only keeping one click hander active for each button.

Instead of:

class View {
    Button commitButton;

    public HasClickHandlers getCommit () {return commitButton;}
}

..and link to this in the Activity:

view.getCommit.addClickHandler(new Clickhandler()...

Do this in the View:

    class View {
        private Button commitButton;        
        private HandlerRegistration commitRegistration = null;

        public void setCommitHandler (ClickHandler c) {
            commitRegistraion != null ? commitRegistration.removeRegistration ();
            commitRegistration = commitButton.addClickHandler (c);
        }
    }

And the Activity:

view.setCommitHandler (new ClickHandler () ...

Hope that helps.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文