MVP HasClickHandler

341 views
Skip to first unread message

fonghuangyee

unread,
Sep 6, 2009, 4:19:59 AM9/6/09
to Google Web Toolkit
Hihi ,
After i go through so many MVP example, i found that we always use
presenter to get those HasClickHandler from Display( Or View) to do
some action. This way works nice for static clickable action, but if
we have some dynamic button ( example a table listing with a list of
clickable action ), then i have to get a list of HasClickHandler
from
display? and every time i add new row to the table listing, i have to
take care of the handler action, quite troublesome.
any advice?

Thanks.

Jason A. Beranek

unread,
Sep 6, 2009, 8:32:37 PM9/6/09
to Google Web Toolkit
One approach I've used is to put some logic in the View to forward a
different event than the purely GUI events to the Presenter. For
example, if you have a table or list of items which can be clicked,
and a click signifies selection of an item, use HasSelectionHandlers()
in the View, and translate in the View which item is selected by the
click. This seems to work for selection use cases, but it makes your
View in the MVP more Supervising Controller (http://martinfowler.com/
eaaDev/SupervisingPresenter.html) than Passive View (http://
martinfowler.com/eaaDev/PassiveScreen.html).

If you were to need separate handling for each list item being
clicked, then you are likely looking at a Presenter per list item or
at having the View include a factory to add list items and return
handlers (i.e., HasClickHandlers addListItem() ). Any of these
approaches will work, it just depends on what you are looking to do
with each click handler.

V/r,

Jason

fonghuangyee

unread,
Sep 10, 2009, 11:47:42 AM9/10/09
to Google Web Toolkit
Thanks for ur advise.
How about if i define a interface ( View Listener ), then my presenter
implement the View Listener?

Example :

public class MyView extend Composite implement MyPresenter.View {

public interface Listerner {

public void onEdit(String id);
}

.....
.....

Listener listener;

// a List of Button here
button.addClickHandler(new ClickHandler(){

public void onClick(ClickEvent event){
listener.onEdit(id);
}
);

}

public class MyPresenter implement MyView.Listener {

public interface View {

}

public void onEdit(String id){
// Do something here.
};
}

Please correct me if i am following the wrong way.
Thanks.

On Sep 7, 8:32 am, "Jason A. Beranek" <jason.bera...@gmail.com> wrote:
> One approach I've used is to put some logic in the View to forward a
> different event than the purely GUI events to the Presenter. For
> example, if you have a table or list of items which can be clicked,
> and a click signifies selection of an item, use HasSelectionHandlers()
> in the View, and translate in the View which item is selected by the
> click. This seems to work for selection use cases, but it makes your
> View in theMVPmore Supervising Controller (http://martinfowler.com/
> eaaDev/SupervisingPresenter.html) than Passive View (http://
> martinfowler.com/eaaDev/PassiveScreen.html).
>
> If you were to need separate handling for each list item being
> clicked, then you are likely looking at a Presenter per list item or
> at having the View include a factory to add list items and return
> handlers (i.e., HasClickHandlers addListItem() ). Any of these
> approaches will work, it just depends on what you are looking to do
> with each click handler.
>
> V/r,
>
> Jason
>
> On Sep 6, 3:19 am, fonghuangyee <fonghuang...@gmail.com> wrote:
>
>
>
> > Hihi ,
> > After i go through so manyMVPexample, i found that we always use
> > presenter to get thoseHasClickHandlerfrom Display( Or View) to do

Jason A. Beranek

unread,
Sep 10, 2009, 7:59:02 PM9/10/09
to Google Web Toolkit
I take a little different approach, but you have most of the
fundamentals. The characteristic interface (i.e., Listener in your
example) shouldn't be defined as part of a particular instance of
MyPresenter.View, as that ties the Presenter directly to a specific
view implementation. Also, I would recommend following the Handler
pattern in GWT, as it allows you to utilize some of the Handler
management functions.

Taking your example, here's a possible approach using
SelectionHandler<String> instead of MyView.Listener:

public class MyView extend Composite implements MyPresenter.View,
HasSelectionHandlers<String> {

HasSelectionHandlers<String> getSelectionSource() {
return this;
}

...

//Do something with a click event (called by the ClickHandler)
void doClick( CLickEvent event ) {
//Retrieve the selection (i.e., item to edit) by processing the
ClickEvent
String selection = getSelectionFromClickEvent( event );
SelectionEvent.fire(this, selection);
}

}

public class MyPresenter implement MyView.Listener {

public interface View {
HasSelectionHandlers<String> getSelectionSource();
}

...

public void bind(View display){
display.getSelectionSource().addSelectionHandler(new
SelectionHandler<String>(){

public void onSelection(SelectionEvent<String> event) {
//Do Something in response to Selection
}

});
};

}

Note that in the example, even though MyView implements the
HasSelectionHandler<String> interface directly, the Presenter only
accesses an instance that HasSelectionHandlers<String> through
View.getSelectionSource(). This allows different View implementations
to implement different sources for the SelectionEvent, reducing
potential coupling between the view and presenter. Hope that helps.

-Jason

fonghuangyee

unread,
Sep 11, 2009, 3:19:20 AM9/11/09
to Google Web Toolkit
Thanks jason,

I just implement HasSelectionHandlers to handle a list of dynamic
hasclickhandler.
It work fine and nice.


On Sep 11, 7:59 am, "Jason A. Beranek" <jason.bera...@gmail.com>
Reply all
Reply to author
Forward
0 new messages