Skip to content

Instantly share code, notes, and snippets.

@typeoneerror
Created January 26, 2020 13:29
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save typeoneerror/fdbf571038f45c056e9f8bc130fbb4d9 to your computer and use it in GitHub Desktop.
// mixins/confirm-transition.js
import Ember from 'ember';
const {
isPresent,
Mixin,
RSVP
} = Ember;
export default Mixin.create({
defaultTransition: null,
doNothingOnConfirm: false,
propertyToCheck: 'model.isNew',
rollbackOnConfirm: false,
transitionConfirmed: false,
activate() {
this._super();
this.set('transitionConfirmed', false);
},
confirmTransition() {
// console.log('confirmTransition');
return RSVP.resolve();
},
actions: {
transitionConfirmed(transition) {
// console.log('transitionConfirmed');
this.confirmTransition().then(() => {
// console.log('transition was confirmed');
const defaultTransition = this.get('defaultTransition');
const model = this.get('controller.model');
const doTransition = () => {
this.set('transitionConfirmed', true);
if (transition) {
transition.retry();
}
else if (isPresent(defaultTransition)) {
this.transitionTo(defaultTransition);
}
};
if (this.get('doNothingOnConfirm')) {
// console.log('doNothingOnConfirm');
doTransition();
}
else if (this.get('rollbackOnConfirm')) {
// console.log('rollbackOnConfirm');
model.rollbackAttributes();
doTransition();
}
else if (model.get('isNew')) {
// console.log('unloadRecord');
this.store.unloadRecord(model);
doTransition();
}
});
},
willTransition(transition) {
// console.log('willTransition');
const property = this.get('propertyToCheck');
if (!this.get('transitionConfirmed') && this.get('controller').get(property)) {
transition.abort();
this.send('showModal', 'confirmTransition', this.get('controller.model'), transition);
}
else {
return true;
}
}
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment