Unbinding presenters necessary in GWT

前端 未结 1 1875
被撕碎了的回忆
被撕碎了的回忆 2021-01-12 16:47

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

I ha

相关标签:
1条回答
  • 2021-01-12 17:28

    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(...));
    }
    
    0 讨论(0)
提交回复
热议问题