简体   繁体   English

GWT:地点,历史记录和MVP示例

[英]GWT: Places, History and MVP by Example

Say I want to create a GWT app that consists of two simple "screens": 假设我要创建一个由两个简单的“屏幕”组成的GWT应用:

  • A Main Menu that users see when the app first starts up (they go to specific URL) - forget login screens, etc., for this simple example; 用户可以在应用程序首次启动时看到的主菜单 (它们进入特定的URL)-对于这个简单的示例,忘记了登录屏幕等; and
  • An Edit Preferences "page"/screen/view that takes the place of the Main Menu when the user clicks a button on the Main Menu; 当用户单击主菜单上的按钮时,将使用“ 编辑首选项 ”页面/屏幕/视图代替主菜单; this Edit Preferences screen also has a button that takes the user back to the Main Menu; 这个“编辑首选项”屏幕上还有一个按钮,可将用户带回到主菜单。 thus 从而
  • The user can simply go back and forth between the Main Menu and Edit Preference "pages" (this is GWT so its actually a single-page app) all day long 用户可以整天在主菜单和“编辑首选项”页面之间来回切换(这是GWT,因此它实际上是一个单页面的应用程序)

So the pseudo-code for this might look like: 因此,此伪代码可能类似于:

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: 问题:

  1. How does a Place and its associated Activities fit into this paradigm? Place及其相关Activities适应此范例? If it's a choice between MVP and Places/Activities, then how would this example look using the Activities/Places paradigm? 如果 MVP和“地点/活动” 之间进行选择,那么使用“活动/地点”范例如何看待本示例?
  2. If MVP and Activities/Places can play nicely together (and are not mutually exclusive), then how could we implement GWT's History API here so that we can set MainMenuView as one "place" in history, EditPreferencesView as a second "place", and then allow the user to hit the Back/Forward browser buttons and keep toggling between them? 如果MVP和活动/场所可以很好地一起玩(并且不互斥),那么我们如何在此处实现GWT的历史记录API,以便我们可以将MainMenuView设置为历史记录中的一个“位置”,将EditPreferencesView为第二个“位置”,并且然后允许用户点击“后退/前进”浏览器按钮并在它们之间继续切换?
  3. If MVP and Activities/Places are mutually exclusive, and I have to choose between the two, is there anything different about the code in Question #2 above that needs to change? 如果MVP和活动/场所是互斥的,并且我必须在两者之间进行选择,那么上述问题2中的代码是否有任何其他需要更改的地方?

I'm also interested in how a Place ties into an ActivityManager , and how/who fires/handles PlaceChangeEvents , but I'll save that for later; 我还对Place如何PlaceChangeEventsActivityManager以及如何/谁触发/处理PlaceChangeEvents ,但我将其保存以备后用。 I need to understand these concepts before I can move on to those. 在继续学习这些概念之前,我需要了解这些概念。 Thanks in advance! 提前致谢!

I'll try to outline an example setup that could work in your situation. 我将尝试概述一个适合您情况的示例设置。 There are lots of variations, but I'd like to concentrate on a simple one (and encourage you to think up where this can be adjusted). 有很多变体,但我想专注于一个简单的变体(并鼓励您考虑可以对此进行调整的地方)。

1. Define URL tokens 1.定义URL令牌

Choose some URL tokens like "#mainmenu" and "#editprefs" which will be appended to the host page's URL. 选择一些URL令牌,例如“ #mainmenu”和“ #editprefs”,这些令牌将附加到主机页面的URL中。 They will be used for the browser history, so the back and forward buttons work etc. 它们将用于浏览器历史记录,因此后退和前进按钮可以使用,等等。

The URL handling will be done automatically for you by DefaultHistorian. URL处理将由DefaultHistorian自动为您完成。 The PlaceChangeEvent will be fired by PlaceController. PlaceChangeEvent将由PlaceController触发。

2. Map tokens to Place objects 2.将令牌映射到放置对象

A Place object is simply the object oriented abstraction of a token - this is useful, because more advanced tokens can also take parameters which need to be parsed. Place对象只是令牌的面向对象的抽象-这很有用,因为更高级的令牌还可以采用需要解析的参数。 You'll need a way to map the tokens to Place objects. 您需要一种将令牌映射到Place对象的方法。 This is the responsibility of PlaceHistoryMapper . 这是PlaceHistoryMapper的责任。

In my example, we'd simply implement PlaceHistoryMapper manually to map "#mainmenu" to a MainMenuPlace and "#editprefs" to an EditPreferencesPlace. 在我的示例中,我们只需手动实现PlaceHistoryMapper即可将“ #mainmenu”映射到MainMenuPlace,将“ #editprefs”映射到EditPreferencesPlace。

[Alternatively, it's also possible to use the @WithTokenizers annotation and implement an (empty) PlaceTokenizer for every type of place. [或者,也可以使用@WithTokenizers批注并为每种类型的场所实现一个(空的)PlaceTokenizer。 You could then use the @Prefix annotation to specify "mainmenu" and "editprefs" as the tokens.] 然后,您可以使用@Prefix批注将“ mainmenu”和“ editprefs”指定为标记。]

3. Map the Places to Activities 3.将地点映射到活动

A Place object itself doesn't do anything - as explained above, it's basically just an abstract token. Place对象本身不执行任何操作-如上所述,它基本上只是一个抽象标记。 The actual code will be run in an Activity. 实际的代码将在Activity中运行。 So you'll have to map Places to Activities. 因此,您必须将“地方”映射到“活动”。 This is the responsibility of ActivityMapper . 这是ActivityMapper的责任。

In my example you'd implement it to map MainMenuPlace to a MainMenuActivity and EditPreferencePlace to an EditPreferenceActivity. 在我的示例中,您将实现将MainMenuPlace映射到MainMenuActivity并将EditPreferencePlace映射到EditPreferenceActivity的方法。

4. Activities and Presenters 4.活动和演讲者

For simplicity, in my example the Activities would also implement the Presenter. 为简单起见,在我的示例中,“活动”也将实现“演示者”。 So MainMenuActivity would implement MainMenuPresenter. 因此MainMenuActivity将实现MainMenuPresenter。 This is not necessary at all, but maybe a nice starting point. 这根本没有必要,但也许是一个不错的起点。 And this is where Places+Activities can connect with MVP. 这是Places + Activities可以与MVP连接的地方。 The two concepts don't require each other, but they work nicely together: 这两个概念并不需要,但是它们可以很好地协同工作:

  • Activities+Places is basically about the connection between the history token and an Activity. 活动+地点基本上是关于历史记录令牌和活动之间的联系。
  • MVP is basically about the connection between a Presenter and a View. MVP基本上是关于Presenter和View之间的连接。

If you let an Activity (or one of its delegates) implement a Presenter, you have connected both. 如果您让一个Activity(或其一个委托)实现一个Presenter,则您将两者连接在一起。

5. Quick overview 5.快速概述

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM