better mapTransitions

prevents some unhandled cases
This commit is contained in:
Pooya Parsa 2017-06-13 00:06:05 +04:30
parent 0511e90a70
commit e753f93f97

View File

@ -14,21 +14,25 @@ let router
<%= (store ? 'let store' : '') %> <%= (store ? 'let store' : '') %>
function mapTransitions(Components, to, from) { function mapTransitions(Components, to, from) {
const resolveTransitions = component => (typeof component.options.transition === 'function') function componentTransitions(component) {
? component.options.transition(to, from) if (!component || !component.options || !component.options.transition) {
: component.options.transition return {}
}
const resolveRoute = route => resolveTransitions(route.matched[0].components.default) if(typeof component.options.transition === 'function') {
return component.options.transition(to, from)
return Components.map((Component) => { }
const transitions = Object.assign({}, to ? resolveRoute(to) : resolveTransitions(Component)) return component.options.transition
const from_transitions = from ? resolveRoute(from) : {} }
// Combine transitions & prefer leave* transitions of from route return Components.map((Component) => {
Object.keys(from_transitions).forEach(key=> { // Clone original object to prevent overrides
if (from_transitions[key] && key.toLowerCase().indexOf('leave') !== -1) { const transitions = Object.assign({}, componentTransitions(Component))
transitions[key] = from_transitions[key] // Combine transitions & prefer *leave* transitions of 'from' route
if(from && from.matched.length && from.matched[0].components.default) {
const from_transitions = componentTransitions(from.matched[0].components.default)
Object.keys(from_transitions)
.filter(key => from_transitions[key] && key.toLowerCase().indexOf('leave') !== -1)
.forEach(key => { transitions[key] = from_transitions[key] })
} }
})
return transitions return transitions
}) })
} }