Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prevent route change in vaadin router in middle of some other task. #448

Open
Krishuthapa opened this issue Mar 31, 2020 · 3 comments
Open

Comments

@Krishuthapa
Copy link

Krishuthapa commented Mar 31, 2020

I am using vaadin router in LitELement. I have a vaadin upload component on one page. While uploading the content, if by mistakenly I change the route. In that case, I need to show a confirmation popup for the user. So how do I prevent this route change behavior by default and carry on the action depending on the option selected by the user in the confirmation?

Somewhat like onbeforeunload which works only when reloading the page, is there any event or method in vaadin to mimic that kind of feature in route change.

I don't want this feature for all the route changes. This should just be triggered whenever I am in middle of doing something and mistakenly I change the route.

@haijian-vaadin
Copy link
Contributor

Hi @Krishuthapa, good to hear that you are using vaadin-router. For your use case, I think you can use the onBeforeLeave lifecycle callback. You can find the documentation here. We also have a helper interface BeforeLeaveObserver to make it typesafe, for example, you can define your view like

class MyViewWithBeforeLeave extends LitElement implements BeforeLeaveObserver {
  onBeforeLeave(
      location: RouterLocation,
      commands: PreventCommands,
      router: Router) {
        if (!window.confirm('Are you sure you want to leave?')) {
          return commands.prevent();
        }
  }
}

@Krishuthapa
Copy link
Author

@haijian-vaadin thank you very much for the reply. I will try and see if it works for my use case.

@malindalin
Copy link

@haijian-vaadin I see the docs mention commands.prevent will abort the navigation and fall back to the last one, will the router location object also update itself to match the navigation/location it's falling back to?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants