ember-learn / ember-learn/guides-source

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

Open
#2,077 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
HTML
Stars
161
Forks
512
Avg merge
4d 9h
Merged PRs (30d)
4

Description

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.

```javascript
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?

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.