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 component.options.transition
}
return Components.map((Component) => { return Components.map((Component) => {
const transitions = Object.assign({}, to ? resolveRoute(to) : resolveTransitions(Component)) // Clone original object to prevent overrides
const from_transitions = from ? resolveRoute(from) : {} const transitions = Object.assign({}, componentTransitions(Component))
// Combine transitions & prefer leave* transitions of from route // Combine transitions & prefer *leave* transitions of 'from' route
Object.keys(from_transitions).forEach(key=> { if(from && from.matched.length && from.matched[0].components.default) {
if (from_transitions[key] && key.toLowerCase().indexOf('leave') !== -1) { const from_transitions = componentTransitions(from.matched[0].components.default)
transitions[key] = from_transitions[key] 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
}) })
} }