Wednesday, October 20, 2010

GWT 2.1 RC-1 MVP introduction

I used GWT at 1.2.x for a while and did a little project on that. and then never use GWT too much. Recently, GWT 2.1 RC 1 is released. I think why not pick it up. This blog aims to summary some related infos and my tips on GWT2.1 RC 1 MVP (Modle-View-Presenter) subject.

Since Ran Ryan's gwt best practices talk at Google IO 09, the gwt community has some implementations over the MVP ideas: GWTP and MVP4g . GWT team decided to put MVP into the gwt-2.1, named as MVP framework (Activity and Place)

As every other new framework or technology, GWT MVP framework has many terms and some of them are not so intuitive. However, Google alway offers documentations and examples.

Here is a list of google references on this subject:

Sample app helloMVP

Large Scale app development and MVP part-I and part-II

How to use google-gin in the helloMVP example (on-going discussion on google group)

Thomas Broyer on his blog wrote a serie of article over the MVP concept

In this blog, I am going to try explain the concept very briefly and give a example.

GWT-MVP has three major concepts: Place, Activity, and View.

Place:
one place is one bookmark-able URL of your GWT app, each place has its own String token, and each place can be accessed via a URL. For example, you application URL is localhost:8888, your place A's string token is "home", the URL for this place is localhost:8888#home.
Place is just a java class, you put state information into it.

Activity:

IMO really a bad naming, Activity is a presenter in MVP term. one place should point to a activity. It is possible to point multiple place to the same activity. As mentioned, place can contains state, so you choose activity to response different state.

View:
In GWT MVP, Activity supposes to fetch the view, and add the view to the page.

so Place-->Act-->View-->Other Places

to glue act and place together, we need PlaceHistoryMapper , ActivityMapper, and ActivtiyManager. PlaceHistroyMapper is mapping string token to place, ActivtiyMapper is mapping place to activity, and ActivityManager is charge off handle the PlaceChangeEvent and execute the activity.

Next blog will give a example using gwt mvp framework