View with UiHandlers
Bottom-Top Communication: ViewWithUiHandlers
In any MVP application, there will be a time when a user event needs to trigger some kind of computation or succession of other events. The way to link a view event to a Presenter is with the Bottom-Top Communication pattern.
When the need of a View-To-Presenter communication arises, it is now time to have the View implement HasUiHandlers
. In GWTP, there's an abstract class that does that already, it is called ViewWithUiHandlers
.
The first step is to define an interface that extends UiHandlers
that describes what actions the current Presenter can do. Let's imagine a View-Presenter couple of a screen where a user can save a user's information on the system. First declare the UiHandlers:
Then let the Presenter know it'll have to handle this action:
It is also necessary to let the View know it has access to the UiHandlers. So at the View interface declaration :
Finally, when implementing the view, the class signature changes a little bit as well.
So now the class structure is well established. To get it working in a real case though, the Presenter has to set itself as the view's UiHandler at runtime by calling the setUiHandlers
method in its constructor.
Voila! The view can then communicate with its Presenter and send whatever data it needs to:
Last updated
Was this helpful?