From 4c205712689144616e99329bce75d09c364f9fed Mon Sep 17 00:00:00 2001 From: Randall Morey Date: Mon, 3 Jan 2022 13:59:24 -0500 Subject: [PATCH] =?UTF-8?q?refactor:=20=F0=9F=92=A1=20refactor=20due=20to?= =?UTF-8?q?=20router=20events=20deprecations?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ui/admin/app/routes/application.js | 50 ++++++++++++++++-------------- 1 file changed, 27 insertions(+), 23 deletions(-) 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)