使用 MVP 模式在 GWT 中保留导航/历史状态
我已经实现了一个基于 MVP 模式 Google 的基本 GWT 应用推荐。我试图弄清楚的是,一旦您用数据填充应用程序,存储导航/历史状态的最佳方法。
假设您有一个搜索将一堆数据返回到 CellTable 中。如果我导航到另一个面板的搜索结果中的特定项目,则带有搜索结果的初始面板就会消失,除非演示者/视图存储在某处,以便我可以在后退导航中轻松访问它。
所以,我的问题是,像 Gmail 这样的应用程序如何保留后退导航的状态?有没有例子说明如何实现这一点?
I've implemented a basic GWT app based on on the MVP pattern Google recommends. What I'm trying to figure out is the best way to store navigation/history state once you fill your application with data.
Let's say you have a search that returns a bunch data into a CellTable. If I navigate to a specific item in the search result to another panel, the initial Panel with the search result is gone unless the Presenter/View is stored somewhere so I can access it easily on a back navigation.
So, my question is, what do apps like Gmail do to preserve the state for back navigation? Are there any examples of how this can be implemented?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
Gmail 不使用 GWT,因此我假设您只是想要一个高级答案。
Gmail 使用 URL 片段(
#
后面的部分)。当您在 Gmail 中导航时,您会注意到该片段会更改为 Gmail 导航中每个“位置”的唯一标识符。使用片段可以使浏览器为您完成所有跟踪,而无需重新加载页面。然后,您只需监视该片段,当它发生更改时,您将导航到它指定的位置。Gmail doesn't use GWT, so I'm assuming you just want a high-level answer.
Gmail uses the URL fragment (the part after the
#
). As you navigate around in Gmail you'll notice that the fragment changes to a unique identifier for each "location" in Gmail's navigation. Using the fragment makes the browser do all the tracking for you without requiring page reloads. You then just monitor the fragment, and when it changes you navigate to the location it specifies.GWT 有多个 MVP 库项目,它们使用 Place 的概念来表示 Presenters 的状态。 Place 实现通常将状态映射到 # 之后的 URL 片段。因此,它们的工作方式与 Gmail 的状态处理类似。
例如,使用 gwt-presenter 项目,您可能有一个DataPresenter 和 DataPlace:
当 URL 的格式为 /data#state=12345 时,将要求此 Place 根据参数准备 Presenter。之后,Presenter中的reveal方法将被调用。由于 Place 已准备好状态,因此您将能够根据需要恢复视图。
There are several MVP library projects for GWT that use the concept of a Place to represent the state of Presenters. Place implementations generally map the state to the URL fragment after the #. Hence, they work similarly to Gmail's state handling.
As an example, using the gwt-presenter project, you may have a DataPresenter and a DataPlace:
When the URL has the form /data#state=12345, this Place will be asked to prepare the Presenter based on the parameters. Afterwards, the reveal method in the Presenter will be invoked. Since the state was already prepared by the Place, you'll be able to restore the view as needed.
您在哪里创建活动?您应该返回一个现有的活动,而不是每次地点发生变化时创建一个新的活动。活动通常在
ActivityMapper
中创建。您有两个选择:更改
ActivityMapper
,以便它在第一次调用时创建一个Activity
实例,并在后续调用时返回该实例。或者,使用
CachingActivityMapper
来包装您的ActivityMapper
。它将返回现有的Activity
,而不是创建新的活动。Where do you create Activities? You should return an existing activity instead of creating a new one every time a place changes. Activities are usually created in
ActivityMapper
. You have two options:Change
ActivityMapper
so that it creates anActivity
instance on first invocation, and return this instance on subsequent invocations. Or,Use
CachingActivityMapper
to wrap yourActivityMapper
. It will return an existingActivity
instead of creating a new one.