diff --git a/ui/admin/app/routes/application.js b/ui/admin/app/routes/application.js index 75b12751c..ef413c97b 100644 --- a/ui/admin/app/routes/application.js +++ b/ui/admin/app/routes/application.js @@ -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)