refactor: 💡 refactor due to router events deprecations

This commit is contained in:
Randall Morey committed 2022-01-03 13:59:24 -05:00
1 parent af92c65f60
commit 4c20571268
1 file changed
+27 -23
+27 -23
View File
@@ -13,6 +13,7 @@ export default class ApplicationRoute extends Route {
@service session;
@service confirm;
@service router;
// =attributes
@@ -20,6 +21,32 @@ export default class ApplicationRoute extends Route {
// =methods
constructor() {
super(...arguments);
/**
* If user attempts to navigate away from unsaved changes, the user is
* asked to confirm that they would like to discard the changes. If the
* user chooses discard, changes on the model are rolled back and the
* transition is retried. If the user cancels discard, the transition is
* aborted.
*/
this.router.on('routeWillChange', async (transition) => {
const fromName = transition?.from?.name;
const toName = transition?.to?.name;
const maybeModel = transition?.from?.attributes;
if (fromName !== toName && maybeModel?.hasDirtyAttributes) {
transition.abort();
try {
await this.confirm.confirm('abandon', { isAbandonConfirm: true });
maybeModel?.rollbackAttributes();
transition.retry();
} catch (e) {
// if user denies, do nothing
}
}
});
}
beforeModel() {
const theme = this.session.get('data.theme');
this.toggleTheme(theme);
@@ -63,29 +90,6 @@ export default class ApplicationRoute extends Route {
return true;
}
/**
* If user attempts to navigate away from unsaved changes, the user is
* asked to confirm that they would like to discard the changes. If the
* user chooses discard, changes on the model are rolled back and the
* transition is retried. If the user cancels discard, the transition is
* aborted.
* @param {Transition} transition
*/
@action
async willTransition(transition) {
const maybeModel = transition?.from?.attributes;
if (maybeModel?.hasDirtyAttributes) {
transition.abort();
try {
await this.confirm.confirm('abandon', { isAbandonConfirm: true });
maybeModel?.rollbackAttributes();
transition.retry();
} catch (e) {
// if user denies, do nothing
}
}
}
/**
* Applies the specified color theme to the root ember element.
* @param {string} theme - "light", "dark", or nullish (system default)