Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
// 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