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

Include teardown of this.router.on in "Preventing and Retrying Transitions" #2077

Open
deanmarano opened this issue Nov 20, 2024 · 0 comments

Comments

@deanmarano
Copy link

In the guides, it is suggested to use this.router.on as a way to prevent a route transition:

https://guides.emberjs.com/release/routing/preventing-and-retrying-transitions/#toc_preventing-transitions-via-routewillchange

However, this listener is never torn down, and will fire for all transitions in the app, regardless if they are from or to the current route.

export default class FormRoute extends Route {
  @service router;

  constructor() {
    super(...arguments);

    this.router.on('routeWillChange', (transition) => {
      if (!transition.to.find(route => route.name === this.routeName) && 
        !confirm('Are you sure you want to abandon progress?')) {
        transition.abort();
      }
    });
  }
};

I'd like to open a PR to include information about using router.off here. Would that be helpful?

@deanmarano deanmarano changed the title Teardown of this.router.on usage in "Preventing and Retrying Transitions" Include teardown of this.router.on in "Preventing and Retrying Transitions" Nov 20, 2024
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

1 participant