From 94fe3236d7242018eb03c3d6fb77d6cff331f1a5 Mon Sep 17 00:00:00 2001 From: Matthew Kilgore Date: Fri, 7 Aug 2020 20:40:31 -0400 Subject: [PATCH] infrastructure: Add routing to common/Application --- js/src/common/Application.js | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/js/src/common/Application.js b/js/src/common/Application.js index aa115ad30..1616918ab 100644 --- a/js/src/common/Application.js +++ b/js/src/common/Application.js @@ -195,7 +195,7 @@ export default class Application { // this.drawer = new Drawer(); - // m.route(document.getElementById('content'), basePath + '/', mapRoutes(this.routes, basePath)); + m.route(document.getElementById('content'), basePath + '/', mapRoutes(this.routes, basePath)); // Add a class to the body which indicates that the page has been scrolled // down. @@ -432,9 +432,19 @@ export default class Application { * @public */ route(name, params = {}) { - const url = this.routes[name].path.replace(/:([^\/]+)/g, (m, key) => extract(params, key)); - const queryString = m.route.buildQueryString(params); - const prefix = m.route.mode === 'pathname' ? app.forum.attribute('basePath') : ''; + const route = this.routes[name]; + + if (!route) throw new Error(`Route '${name}' does not exist`); + + const url = route.path.replace(/:([^\/]+)/g, (m, key) => extract(params, key)); + + // Remove falsy values in params to avoid having urls like '/?sort&q' + for (const key in params) { + if (params.hasOwnProperty(key) && !params[key]) delete params[key]; + } + + const queryString = m.buildQueryString(params); + const prefix = m.route.prefix === '' ? this.forum.attribute('basePath') : ''; return prefix + url + (queryString ? '?' + queryString : ''); }