2011-10-06 32 views
1

我正在使用GWTs活动和地方几乎所述http://code.google.com/webtoolkit/doc/latest/DevGuideMvpActivitiesAndPlaces.html,它都工作正常。使用GWT活动和地点我可以导航到前一页而不使用History.back()?

我想要做的是从特定页面导航到前一页,而不使用History.back(),因为我不想丢失历史状态。 (我有一个页面,用户执行一个动作,成功时我想返回到前一页并保持历史状态,另一方面,如果他们取消,我想用History.back(),因为我想要失去状态) 。

我能想到的要做到这一点的唯一方法是创建一个侦听地点/历史变化事件,使现有的以前的地方我我自己的地方/历史跟踪代码,这样我可以调用placeController.goto(...)

有没有更简单的方法来做到这一点?我错过了什么吗?

回答

1

你必须记得要返回,因为不是取消用户可以点击后退按钮,这将与点击取消相同,除非你的应用程序没有执行代码,所以你没有控制。

其次,如果您在网址中有历史记录,则用户可以直接导航到该页面,然后在用户单击确定时应该知道该去哪儿。或者如果用户直接进入该页面,则将用户重定向到另一个页面。

一种方法是将返回历史记录标记存储在要访问的页面的历史记录标记中。当页面完成后,它可以根据传递的返回令牌返回(或者从技术上讲,它将'前进')到该页面。 (尽管使用GWT,您可以轻松地将代码中的历史记号存储起来)。

2

我采取的方法是在代码中存储历史记号(如建议)。我扩展了PlaceController并用它来跟踪EventBus上的Place变化。现在无处不在我使用PlaceController,而是使用PlaceControllerExt,它有一个很好的previous()方法,可以将我带回我来自的地方 - 但向前导航,永远不会离开应用程序。

public class PlaceControllerExt extends PlaceController { 

    private final Place defaultPlace; 
    private Place previousPlace; 
    private Place currentPlace; 

    public PlaceControllerExt(EventBus eventBus, Place defaultPlace) { 

     super(eventBus); 
     this.defaultPlace = defaultPlace; 
     eventBus.addHandler(PlaceChangeEvent.TYPE, new PlaceChangeEvent.Handler() { 

      public void onPlaceChange(PlaceChangeEvent event) { 

       previousPlace = currentPlace; 
       currentPlace = event.getNewPlace(); 
      } 
     }); 
    } 

    /** 
    * Navigate back to the previous Place. If there is no previous place then 
    * goto to default place. If there isn't one of these then it'll go back to 
    * the default place as configured when the PlaceHistoryHandler was 
    * registered. This is better than using History#back() as that can have the 
    * undesired effect of leaving the web app. 
    */ 
    public void previous() { 

     if (previousPlace != null) { 
      goTo(previousPlace); 
     } else { 
      goTo(defaultPlace); 
     } 
    } 
} 
相关问题