[英]GWT: Places, History and MVP by Example
Say I want to create a GWT app that consists of two simple "screens": 假设我要创建一个由两个简单的“屏幕”组成的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: 问题:
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? 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? MainMenuView
设置为历史记录中的一个“位置”,将EditPreferencesView
为第二个“位置”,并且然后允许用户点击“后退/前进”浏览器按钮并在它们之间继续切换? 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
如何PlaceChangeEvents
到ActivityManager
以及如何/谁触发/处理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).
有很多变体,但我想专注于一个简单的变体(并鼓励您考虑可以对此进行调整的地方)。
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触发。
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”指定为标记。]
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的方法。
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:
这两个概念并不需要,但是它们可以很好地协同工作:
If you let an Activity (or one of its delegates) implement a Presenter, you have connected both. 如果您让一个Activity(或其一个委托)实现一个Presenter,则您将两者连接在一起。
"#mainMenu"
---(PlaceHistoryMapper)---> MainMenuPlace
---(ActivityMapper)---> MainMenuActivity implements MainMenuPresenter
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.