1
0
mirror of https://github.com/flarum/core.git synced 2025-08-08 09:26:34 +02:00

Introduce setRouteWithForcedRefresh util

This commit is contained in:
Alexander Skvortsov
2020-08-24 20:23:43 -04:00
committed by Franz Liedke
parent 2edbd4508a
commit 038744f092
3 changed files with 21 additions and 3 deletions

View File

@@ -1,3 +1,4 @@
import setRouteWithForcedRefresh from '../utils/setRouteWithForcedRefresh';
import SearchState from './SearchState';
export default class GlobalSearchState extends SearchState {
@@ -66,7 +67,7 @@ export default class GlobalSearchState extends SearchState {
params.sort = sort;
}
m.route.set(app.route(this.searchRoute, params), null, { state: { key: Date.now() } });
setRouteWithForcedRefresh(app.route(this.searchRoute, params));
}
/**
@@ -90,6 +91,6 @@ export default class GlobalSearchState extends SearchState {
const params = this.params();
delete params.q;
m.route.set(app.route(this.searchRoute, params), null, { state: { key: Date.now() } });
setRouteWithForcedRefresh(app.route(this.searchRoute, params));
}
}

View File

@@ -1,3 +1,5 @@
import setRouteWithForcedRefresh from './setRouteWithForcedRefresh';
/**
* The `History` class keeps track and manages a stack of routes that the user
* has navigated to in their session.
@@ -114,6 +116,6 @@ export default class History {
home() {
this.stack.splice(0);
m.route.set('/', null, { state: { key: Date.now() } });
setRouteWithForcedRefresh('/');
}
}

View File

@@ -0,0 +1,15 @@
import Mithril from 'mithril';
/**
* Mithril 2 does not completely rerender the page if a route change leads to the same route
* (or the same component handling a different route). This util calls m.route.set, forcing a reonit.
*
* @see https://mithril.js.org/route.html#key-parameter
*/
export default function setRouteWithForcedRefresh(route: string, params = null, options: Mithril.RouteOptions = {}) {
const newOptions = { ...options };
newOptions.state = newOptions.state || {};
newOptions.state.key = Date.now();
m.route.set(route, params, newOptions);
}