Search code examples
gwt-mvpmvp4g

Warn the user that he is about to loose his change in Edit view when leaving to view to another in GWT


I want to prevent the user that he will loose his changes in an EditView when changing the view to another.

I use MVP4G in my project and the project is divided as mvp's structure (one package for the template another one for views ..) is there any solution to detect the EditView in the eventBus. or detect the current View shown to user

Thanks in advance


Solution

  • Thanks to the Navigation Event feature in mvp4g, the presenter will get control before the view changes. At this point the presenter can decide if the navigation will be done or not. This is the correct place in a mvp4g application to save your data.

    First zu have to mark all events in the eventbus that will change your view with:

    @Event(..., navigationEvent = true)
    void goToPage1();
    

    Next your presenters have to implement the NavigationConfirmationInterface and the requires confirm-method:

    public class Presenter extends ... implements NavigationConfirmationInterface {
         public void confirm(NavigationEventCommand event) {
              //pseudo method to verify if the view has changed
              if (isViewModified(){
                   //Window shouldn't be used inside a presenter
                   //this is just to give a simple example
                   if (Window.confirm("Are you sure you want to leave?")){
                        event.fireEvent();
                   }                                
              } else {
                   event.fireEvent();
              }
         }
    }
    

    And the last thing to do, is to set the presenter of the current view to the confirmation presenter by calling:

    event.fireEvent(false);
    

    This is usually done when the presenter gets control.

    You will find the documentation here:

    https://github.com/FrankHossfeld/mvp4g/wiki/03.-Defining-EventBus#navigation-event