1
votes

What would be the MVP approach for using GWT's ClickHandler?

I have a button in my view, which I would like to add a click handler to.

View button.addClickHandler(?)

What gets passed in? The presenter? newed up click handler?

Example case 1. View code:

this.myButton.addClickHandler(new ClickHandler()
    {
        @Override
        public void onClick(ClickEvent event)
        {
            myPresenter.buttonClicked();
        }
    });

In this case, adding a click handler to the button can't be tested...

Example case 2. Presenter code:

this.view.addClickHandlerToButton(this);

    @Override
    public void onClick(ClickEvent event)
    {
        buttonClicked();
    }

In this case, GWT code (ClickEvent) is introduced in the presenter, which should be avoided.

2

2 Answers

2
votes

i solved this this way:

register view to clickevent, and handle it in view:

onClick(){ presenter.onButtonClicked(); }

probably the button has some semantic like "deleteEntry" so the presenters method would be "onDeleteEntryClicked" or "deleteEntry"

mvp says let view decide what kind of ui element to use and presenter executes the command. so the logic behind "deleteEntry" would not change while you could substitute the button by other ui element -also without hasClickHandler.

-1
votes

The button implements an interface called HasClickHandlers, which has a method for registering click events.

In your View interface you can return this interface to your presenter which can call addClickHandler on it.

View interface:

public MyView extends IsWidget{
    public HasClickHandlers getButton();

}

your view implementation:

public HasClickHandler getButton(){
    return button;
}

In your presenter:

view.getButton().addClickHandler(new ClickHandler(){
....
});