mirror of
https://github.com/flarum/core.git
synced 2025-10-14 16:34:26 +02:00
Allow extensions to use route resolvers (#2275)
- mapRoutes: don't wrap components in resolvers if they are already resolvers - Extract defaultResolver into its own class - Allow either route resolver instances, or components with an optional resolverClass which should accept the component and route name in its constructor. - Introduce a resolver for DiscussionPage, so that routing from one post to another on the same discussion triggers a scroll instead of rerendering
This commit is contained in:
committed by
GitHub
parent
c1d91be2f4
commit
988b6c9023
34
js/src/common/resolvers/DefaultResolver.ts
Normal file
34
js/src/common/resolvers/DefaultResolver.ts
Normal file
@@ -0,0 +1,34 @@
|
||||
import Mithril from 'mithril';
|
||||
|
||||
/**
|
||||
* Generates a route resolver for a given component.
|
||||
* In addition to regular route resolver functionality:
|
||||
* - It provide the current route name as an attr
|
||||
* - It sets a key on the component so a rerender will be triggered on route change.
|
||||
*/
|
||||
export default class DefaultResolver {
|
||||
component: Mithril.Component;
|
||||
routeName: string;
|
||||
|
||||
constructor(component, routeName) {
|
||||
this.component = component;
|
||||
this.routeName = routeName;
|
||||
}
|
||||
|
||||
/**
|
||||
* When a route change results in a changed key, a full page
|
||||
* rerender occurs. This method can be overriden in subclasses
|
||||
* to prevent rerenders on some route changes.
|
||||
*/
|
||||
makeKey() {
|
||||
return this.routeName + JSON.stringify(m.route.param());
|
||||
}
|
||||
|
||||
onmatch(args, requestedPath, route) {
|
||||
return this.component;
|
||||
}
|
||||
|
||||
render(vnode) {
|
||||
return [{ ...vnode, routeName: this.routeName, key: this.makeKey() }];
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user