I am using GWT MVP to develop an application. I saw we can have two kinds of event handlers in GWT MVP code, but I am not very sure about which kind should I use in which place:
1) HandlerManager (eventBus) EventHandlers (e.g. EditEventHandler
below) in AppController
:
eventBus.addHandler(EditEvent.TYPE,
new EditEventHandler() {
public void onEdit(EditEvent event) {
doEdit(event.getId()); // this would open a new screen, call AsyncService etc
}
});
I understand, these kind of event handlers are used for handling application-wide custom events.
2) GUI/View Event Handlers (e.g. ClickHandler
) in Presenter
, where I handle the GUI event and then fire the application event to invoke its handler as below:
display.getList().addClickHandler(new ClickHandler() {
public void onClick(ClickEvent event) {
int selectedRow = display.getClickedRow(event);
if (selectedRow >= 0) {
String id = myDetails.get(selectedRow).getId();
eventBus.fireEvent(new EditEvent(id)); // this in turn would invoke the EditEventHandler above
}
}
});
Now, my questions are:
1) Why do we need to write EventHandler for application events (e.g. EditEvent
), and not add that code directly in the associated GUI event handler (e.g. addClickHandler
)?
2)Can't we write the code for opening new screen, calling AsyncService etc directly in GUI EventHandler method, such as onClick
?
3) Wouldn't that make your code more readable, as the event that is triggered and the work that needs to be done is all in one place i.e.Presenter
and you don't have to go back and forth between your Presenter
code and AppController
code?