Skip to content

Commit

Permalink
Extract defaultResolver into it's own util
Browse files Browse the repository at this point in the history
  • Loading branch information
askvortsov1 committed Aug 27, 2020
1 parent a5dd2c6 commit 11b0941
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 7 deletions.
2 changes: 2 additions & 0 deletions js/src/common/compat.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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,
Expand Down
17 changes: 17 additions & 0 deletions js/src/common/utils/defaultResolver.ts
Original file line number Diff line number Diff line change
@@ -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;
},
};
}
10 changes: 3 additions & 7 deletions js/src/common/utils/mapRoutes.js
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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);
}
}

Expand Down

0 comments on commit 11b0941

Please sign in to comment.