Created
June 23, 2021 17:22
-
-
Save th3hamburgler/e0b368da13ca1858bf719a05b83bbea1 to your computer and use it in GitHub Desktop.
will-transition-custom
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Controller from '@ember/controller'; | |
import {action} from '@ember/object'; | |
import {tracked} from '@glimmer/tracking'; | |
export default class FormController extends Controller { | |
// Tracking | |
@tracked showExitModal = false; | |
// Actions | |
@action confirmExit() { | |
// check we have a previous transition to retry | |
if (this.previousTransition) { | |
// retry the previous route transition | |
// this time the modal is active so the routes | |
// willTransition action will let us pass. | |
this.previousTransition.retry(); | |
} | |
} | |
@action cancelExit() { | |
//Clear the modal and transition | |
this.showExitModal = false; | |
this.previousTransition = null; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Route from '@ember/routing/route'; | |
import {action} from '@ember/object'; | |
export default class FormRoute extends Route { | |
// Actions | |
@action willTransition(transition) { | |
const controller = this.controller; | |
// is the exit modal visible? | |
if (!controller.showExitModal) { | |
// show modal to ask the user to confirm exit | |
controller.showExitModal = true; | |
// save the current transition to retry later | |
controller.previousTransition = transition; | |
// stop the current transition | |
transition.abort(); | |
} else { | |
// modal is visible - theirfor we must assume they have | |
// confirmed they want to leave - clear modal and transition state | |
controller.showExitModal = false; | |
controller.previousTransition = null; | |
// Bubble the `willTransition` action so that | |
// parent routes can decide whether or not to abort. | |
return true; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment