Skip to content

Instantly share code, notes, and snippets.

@th3hamburgler
Created June 23, 2021 17:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save th3hamburgler/e0b368da13ca1858bf719a05b83bbea1 to your computer and use it in GitHub Desktop.
Save th3hamburgler/e0b368da13ca1858bf719a05b83bbea1 to your computer and use it in GitHub Desktop.
will-transition-custom
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;
}
}
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;
}
}
}
<form>
...Forms and Forms
</form>
<ExitModal
@isShowing={{this.showExitModal}}
@onConfirm={{this.confirmExit}}
@onCancel={{this.cancelExit}}
/>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment