Best Practices for binding and unbinding presenters

569 views
Skip to first unread message

DrJ

unread,
Feb 21, 2012, 12:08:14 PM2/21/12
to gwt-pl...@googlegroups.com
 Below I have created a handler so a control can listen for changes to date range criteria at the top application header.  Since the presenter below should only respond to events when it is visible.  I have setup the control as per below.  Whilst developing this code I wondered what the purpose of the onBind event is?  Since it is only ever called once - when the presenter is constructed - it is not usable in a scenario where a singleton presenter is shown and hidden multiple times, as per navigation through any standard screen.  Therefore I wanted to ask two things:

1) Does the code below adhere to best practices?

2) What is the purpose of the onBind events - Is it to do something that you only ever want to do once per presenter?  Such as building the control?

/**
     * Override to handle changes in the date filter
     */
    protected void onNewDateSelected(){}


    private final DateRangeFilterEventHandler dateRangeFilterEventHandler = new DateRangeFilterEventHandler() {
        @Override
        public void onDateRangeFilterChanged(DateRangeFilterEvent eventApp) {
            ABaseCustomerPresenter.this.onNewDateSelected();
        }
    };

    @Override
    protected void onReveal() {
        super.onReveal();

        //Bind handlers with the GWTP addRegisteredHandler so they can easily be unbound when the presenter is hidden
        addRegisteredHandler(DateRangeFilterEvent.getType(), dateRangeFilterEventHandler);
    }

    @Override
    protected void onHide() {
        super.onHide();
        super.unbind(); //unbind any handlers registered with the addRegisteredHandler method.
    }

Asier

unread,
Feb 22, 2012, 1:24:57 AM2/22/12
to gwt-pl...@googlegroups.com

On Martes 21 Febrero 2012 18:08:14 DrJ escribió:


I have the same problem.


I've "solved" it checking in the event handling methods on* if the presenter it's visible, instead of adding handlers/unbinding the presenter, but I don't thinks it's the best solution.


Regards

Christian Goudreau

unread,
Feb 23, 2012, 1:40:22 PM2/23/12
to gwt-pl...@googlegroups.com
Hi Gene,

onBind is mostly used to delay creation time to the last possible moment instead of doing that into the CTOR. I register all my handlers in there and my CTOR always only have assignations.

You should never call unbind in onHide and unless you have heavy objects that you want to destroy, onHide and onUnbind are not useful.

Cheers,
--
Christian Goudreau

Christian Goudreau

unread,
Feb 23, 2012, 1:40:47 PM2/23/12
to gwt-pl...@googlegroups.com
You should almost never have to call...

instead of you should never call :D 
--
Christian Goudreau

DrJ

unread,
Feb 23, 2012, 1:50:50 PM2/23/12
to gwt-pl...@googlegroups.com
Cheers Christian.

The problem I have really stems in what to do when a presenter is detached from the DOM.  I would have thought because I am using the addRegisteredHandler that presenters which have been detached are automatically unbound.  Therefore they won't respond to events.  What I am finding is that the events are being responded to even when the presenter is not currently shown/attached.  I am using GWTP 0.6, so perhaps it is a bug and I should upgrade? or maybe an issue with my coding?

Would love to hear your thoughts?

Cheers
Gene


Christian Goudreau

unread,
Feb 28, 2012, 12:57:54 PM2/28/12
to gwt-pl...@googlegroups.com
It is not a bug, this is the intended behavior, since a presenter that isn't show could be interested to updated itself even if it's not show so that the user can be able to see the updates we he comes back.

What kind of events are they? You can check "if visible", but I need to think a little bit about a clean way to do that.
--
Christian Goudreau

DrJ

unread,
Feb 28, 2012, 1:27:05 PM2/28/12
to gwt-pl...@googlegroups.com
One idea would be to specify an "unbind mechanism" parameter to addRegisteredHandler.  One option, the default and current behaviour, would be "Manual Unbind".  The new option "bind when visible".

Thoughts? - Want me to code it?
Gene

On Tuesday, 28 February 2012 09:57:54 UTC-8, Christian Goudreau wrote:
It is not a bug, this is the intended behavior, since a presenter that isn't show could be interested to updated itself even if it's not show so that the user can be able to see the updates we he comes back.

What kind of events are they? You can check "if visible", but I need to think a little bit about a clean way to do that.

Christian Goudreau

unread,
Feb 28, 2012, 1:30:46 PM2/28/12
to gwt-pl...@googlegroups.com
Hum, that can be interesting, but in the second case, you have to rebind the events when the onReveal is called back.

A fast workarround would be to bind your event normally in onReveal with eventBus().addhandler. Keep the references in a list and remove every event in onHide();

But I like the idea, let's open an issue and see what other users think! :D
--
Christian Goudreau

opn

unread,
Feb 28, 2012, 11:31:22 PM2/28/12
to gwt-pl...@googlegroups.com
I just did exactly that "fast workaround"! Initially thought that addRegisteredHandler() bound events would already be removed and rebound in onHide() onReveal :)

Would be a nice addition!

Alex

Asier

unread,
Feb 29, 2012, 5:22:56 AM2/29/12
to gwt-pl...@googlegroups.com
El 23/02/2012 19:40, Christian Goudreau escribió:
> Hi Gene,
>
> onBind is mostly used to delay creation time to the last possible moment
> instead of doing that into the CTOR. I register all my handlers in there and
> my CTOR always only have assignations.
>
> You should never call unbind in onHide and unless you have heavy objects that
> you want to destroy, onHide and onUnbind are not useful.
>

In my use case I unbind a pretty big PresenterWidget with some hundred child
controls so I think it's appropiate.

But... talking about PresenterWidgets attached to events-

When I create the PW, in it's onBind method I use to do:

addRegisteredHandler( ... )

The problem is that when I hide it and later create it again then the PW gets
registered twice and the event handlers are executed two, three, etc, times. I
can unregister the event handlers in the onHide method, but if you say:

> You should never call unbind in onHide and unless you have heavy objects
> that you want to destroy, onHide and onUnbind are not useful.

What should I do to not have the event handlers registered again?

Regards

opn

unread,
Mar 1, 2012, 2:03:30 AM3/1/12
to gwt-pl...@googlegroups.com, avs...@gmail.com
onBind() is only called once I think.

JavaDoc:


Any event handler should be initialised here rather than in the constructor. Also, it is good practice to perform any costly initialisation here.
Handlers registered by calling registerHandler(HandlerRegistration) will be removed when unbinding. Any other initialisation that takes place here (or as a side-effect of what is done here) should be taken down in onUnbind().
This method will never be invoked more then once, or if it is, the second time will necessarily be preceded by an invocation of onUnbind().

Asier

unread,
Mar 1, 2012, 4:42:54 AM3/1/12
to gwt-pl...@googlegroups.com
El 01/03/2012 8:03, opn escribió:

But on PresenterWidgets its called every time the PW is created, so its
registered each time, at least when using guice Providers and factories mad by
GinFactoryModuleBuilder

regards

Christian Goudreau

unread,
Mar 4, 2012, 11:38:51 AM3/4/12
to gwt-pl...@googlegroups.com
Yes it is called every time, but the handler itself should be called only once in the PW itself that has registered itself.

That being said, could you give more information about what you are doing exactly? Because it seem to me like the handler shouldn't be registered in those PW, but in the parent. Which will end up by creating only one handler controlled by the parent and thus, no more "duplication".

Cheers,
--
Christian Goudreau
Reply all
Reply to author
Forward
0 new messages