Created
January 26, 2020 13:29
-
-
Save typeoneerror/fdbf571038f45c056e9f8bc130fbb4d9 to your computer and use it in GitHub Desktop.
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
// 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