I am writing a web application using GWT, and following the MVP tutorial on the GWT website (i.e. using History for navigation).
I am a bit confused as to the best way of having a side-bar for navigation (i.e. clicking a navigation link changes content in main window. See below)
--------------------
| | |
| nav | main |
| | window |
| | |
| | |
--------------------
One potential way I see of doing this is to declare two <div> tags in the HTML for navigation and content. For example:
@Override
public void onValueChange(ValueChangeEvent<String> event) {
...
if (token.equals("navigation")) {
presenter = new NavigationPresenter(rpcService, eventBus, new NavigationView());
presenter.go(RootPanel.get("navigation"));
}
...
if (token.equals("content")) {
presenter = new ContentPresenter(rpcService, eventBus, new ContentView());
presenter.go(RootPanel.get("content"));
}
...
}
I am unsure if this is the best way of approaching this. (I guess bi-directional communication between the navigation panel and the content window could be done through an EventBus? Is this method flexible when it comes to changing the UI (e.g. for mobile sites))
I was wondering what people would suggest is the best way of going about this. I have read around and often land on discussions about Activities and Places, but as I understand it these are somewhat different to MVP architecture (activities and places is useful for browser history management, although I think my use of History covers that (?))
Any suggestions would be appreciated.
Yes, you are right: Activies and Places are about history and navigation management, a framework inside GWT. MVP is simply an architectural design pattern. You can achieve MVP using such framework as shown in the article you linked.
In your sample, you can do as you say, but I'd make each view a (lazy) singleton in order to avoid any potentially expensive re-creation. Personally I don't like this way, you end up defining your Displays in terms of (so many) HasXxx interfaces.
I think that if history management is important, choosing Activities and Places is almost a no-brainer (at least for me, and at least to define the skeleton of the app). Here you can find a really good article to get you started. And you will see how common your use case is: you have to define displayed regions (you navigation and main areas) that will react (thanks to the activity managers) on place change and create/restart the activities that will in turn update the UI. This way you achieve some sort of app-wide MVP in which the presenters are the activities.
As far as MVP is concerned, there is no best way of doing that: part 1, part 2 and MVP with A&P are simply different ways to achieve the same goal: separation between presentation and business logic, as well as pure junit testing. Simply choose what you like. See this post on groups for reference.
Hope that get you started.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With