diff --git a/js/src/common/compat.js b/js/src/common/compat.js index 3764e7e83f..9fdd139f46 100644 --- a/js/src/common/compat.js +++ b/js/src/common/compat.js @@ -7,6 +7,7 @@ import ItemList from './utils/ItemList'; import mixin from './utils/mixin'; import humanTime from './utils/humanTime'; import computed from './utils/computed'; +import defaultResolver from './utils/defaultResolver'; import Drawer from './utils/Drawer'; import anchorScroll from './utils/anchorScroll'; import RequestError from './utils/RequestError'; @@ -74,6 +75,7 @@ export default { 'utils/mixin': mixin, 'utils/humanTime': humanTime, 'utils/computed': computed, + 'utils/defaultResolver': defaultResolver, 'utils/Drawer': Drawer, 'utils/anchorScroll': anchorScroll, 'utils/RequestError': RequestError, diff --git a/js/src/common/utils/defaultResolver.ts b/js/src/common/utils/defaultResolver.ts new file mode 100644 index 0000000000..6de9805320 --- /dev/null +++ b/js/src/common/utils/defaultResolver.ts @@ -0,0 +1,17 @@ +/** + * Generates a route resolver for a given component, to which it provides + * a key as a 'routeName' attr. + */ +export default function defaultResolver(component, key) { + return { + onmatch(args, requestedPath, route) { + return component; + }, + + render(vnode) { + vnode.attrs.routeName = key; + + return vnode; + }, + }; +} diff --git a/js/src/common/utils/mapRoutes.js b/js/src/common/utils/mapRoutes.js index be8a03e61d..545476e41a 100644 --- a/js/src/common/utils/mapRoutes.js +++ b/js/src/common/utils/mapRoutes.js @@ -1,3 +1,5 @@ +import defaultResolver from './defaultResolver.ts'; + /** * The `mapRoutes` utility converts a map of named application routes into a * format that can be understood by Mithril, and wraps them in route resolvers @@ -17,13 +19,7 @@ export default function mapRoutes(routes, basePath = '') { if ('render' in route.component || 'onmatch' in route.component) { map[basePath + route.path] = route.component; } else { - map[basePath + route.path] = { - render() { - return m(route.component, { - routeName: key, - }); - }, - }; + map[basePath + route.path] = defaultResolver(route.component, key); } }