GWT: Places, History, and MVP Following the Example

Let's say I want to create a GWT application consisting of two simple โ€œscreensโ€:

  • The main menu that users see when they first start the application (they go to a specific URL) - forget about the login screens, etc. for this simple example; and
  • Change settings "page" / screen / view, which replaces the main menu when the user presses a button in the main menu; on the Edit Preferences screen there is also a button that returns the user to the main menu; In this way,
  • The user can simply navigate between the Main menu and the "Preferred edit pages" (this is GWT, therefore its one-page application) throughout the day

So, the pseudocode for this might look like this:

public class MainMenuPresenter implements Presenter { // mainMenuScreen may be a Composite that contains a Button for // switching/navigating to the EditPreferencesView private MainMenuView mainMenuScreen; } public class EditPreferencesPresenter implements Presenter { // editPrefsScreen may be a Composite that contains a Button for // switching/navigating back to the MainMenuView private EditPreferencesView editPrefsScreen; } 

Questions:

  • How does Place and its related Activities fit into this paradigm? If this is a choice between MVP and Places / Activities, then how will this example look with the Activities / Places paradigm?
  • If MVP and Activities / Places can play well together (and are not mutually exclusive), then how can we implement the GWT history API here so that we can set MainMenuView as one "place" in the story, EditPreferencesView as the second "place", and then allow user to hit back / forward browser buttons and continue to switch between them?
  • If MVP and Activities / Places are mutually exclusive, and I need to choose between them, is there something else in the code in question No. 2 above that needs to be changed?

I am also interested in how Place communicates with the ActivityManager , and how / who starts / processes PlaceChangeEvents , but I will save this for later; I need to understand these concepts before I can move on to them. Thanks in advance!

+4
source share
1 answer

I will try to describe an example that may work in your situation. There are many options, but I would like to focus on the simple (and encourage you to come up with where you can configure this).

1. Definition of URL Tokens

Select some URL markers, such as "#mainmenu" and "#editprefs", that will be added to the URL of the main page. They will be used for browser history, so the back and forward buttons work, etc.

URL processing will be done automatically by default by default. PlaceChangeEvent will be launched by PlaceController.

2. Display markers for placing objects

A Place object is just an object-oriented abstraction of a token - this is useful because more advanced markers can also take parameters that need to be analyzed. You will need a way to map markers to Place Objects. This is the responsibility of PlaceHistoryMapper .

In my example, we simply applied PlaceHistoryMapper manually to match "#mainmenu" with MainMenuPlace and "#editprefs" in EditPreferencesPlace.

[Alternatively, you can also use the @WithTokenizers annotation and implement a (empty) PlaceTokenizer for each type of place. You can then use the @Prefix annotation to specify "mainmenu" and "editprefs" as tokens.]

3. Matching places to activities

The Place object itself does nothing - as explained above, it is basically just an abstract token. The actual code will be run in Activity. Thus, you will need to map the places of action. This is the responsibility of ActivityMapper .

In my example, you implement it to map MainMenuPlace with MainMenuActivity and EditPreferencePlace with EditPreferenceActivity.

4. Activities and speakers

For simplicity, in my example, Activities will also implement Presenter. Thus, MainMenuActivity will implement MainMenuPresenter. This is not necessary at all, but perhaps a good starting point. And here Places + Activities can connect to MVP. These two concepts do not require each other, but they work great together:

  • Actions + Places is basically the connection between the history token and activity.
  • MVP is mainly related to the connection between the presenter and the presentation.

If you allow the Office (or one of its delegates) to implement the presenter, you connected both.

5. Short description

 "#mainMenu" ---(PlaceHistoryMapper)---> MainMenuPlace ---(ActivityMapper)---> MainMenuActivity implements MainMenuPresenter 
+5
source

Source: https://habr.com/ru/post/1433067/


All Articles