diff --git a/js/src/common/components/Page.js b/js/src/common/components/Page.js index a0bc67039..3689a906d 100644 --- a/js/src/common/components/Page.js +++ b/js/src/common/components/Page.js @@ -12,15 +12,18 @@ export default class Page extends Component { this.onNewRoute(); - app.drawer.hide(); - app.modal.close(); - /** * A class name to apply to the body while the route is active. * * @type {String} */ this.bodyClass = ''; + + this.currentPath = m.route.get(); + } + + routeHasChanged() { + return this.currentPath !== m.route.get(); } /** @@ -30,8 +33,21 @@ export default class Page extends Component { * adjust the current route name. */ onNewRoute() { + this.currentPath = m.route.get(); + app.previous = app.current; app.current = new PageState(this.constructor, { routeName: this.attrs.routeName }); + + app.drawer.hide(); + app.modal.close(); + } + + onbeforeupdate(vnode, old) { + super.onbeforeupdate(vnode, old); + + if (this.routeHasChanged()) { + this.onNewRoute(); + } } oncreate(vnode) { diff --git a/js/src/forum/components/DiscussionPage.js b/js/src/forum/components/DiscussionPage.js index d54e15a48..af1e225cc 100644 --- a/js/src/forum/components/DiscussionPage.js +++ b/js/src/forum/components/DiscussionPage.js @@ -32,23 +32,9 @@ export default class DiscussionPage extends Page { */ this.near = m.route.param('near') || 0; - this.load(); - - // If the discussion list has been loaded, then we'll enable the pane (and - // hide it by default). Also, if we've just come from another discussion - // page, then we don't want Mithril to redraw the whole page – if it did, - // then the pane would redraw which would be slow and would cause problems with - // event handlers. - if (app.discussions.hasDiscussions()) { - app.pane.enable(); - app.pane.hide(); - } - app.history.push('discussion'); this.bodyClass = 'App--discussion'; - - this.prevRoute = m.route.get(); } onremove() { @@ -95,30 +81,32 @@ export default class DiscussionPage extends Page { ); } - onbeforeupdate(vnode) { - super.onbeforeupdate(vnode); + onNewRoute() { + // If we have routed to the same discussion as we were viewing previously, + // prompt the post stream to jump to the new 'near' param. + // Otherwise, load in a new discussion. The `else` branch will + // be followed when `onNewRoute` is called from `oninit`. + const idParam = m.route.param('id'); + if (this.discussion && idParam && idParam.split('-')[0] === this.discussion.id()) { + const near = m.route.param('near') || '1'; - if (m.route.get() !== this.prevRoute) { - this.onNewRoute(); - this.prevRoute = m.route.get(); + if (near !== String(this.near)) { + this.stream.goToNumber(near); + } - // If we have routed to the same discussion as we were viewing previously, - // cancel the unloading of this controller and instead prompt the post - // stream to jump to the new 'near' param. - if (this.discussion) { - const idParam = m.route.param('id'); + this.near = near; + } else { + super.onNewRoute(); - if (idParam && idParam.split('-')[0] === this.discussion.id()) { - const near = m.route.param('near') || '1'; + this.discussion = null; - if (near !== String(this.near)) { - this.stream.goToNumber(near); - } + this.load(); - this.near = near; - } else { - this.oninit(vnode); - } + // If the discussion list has been loaded, then we'll enable the pane (and + // hide it by default). + if (app.discussions.hasDiscussions()) { + app.pane.enable(); + app.pane.hide(); } } } diff --git a/js/src/forum/components/IndexPage.js b/js/src/forum/components/IndexPage.js index 7995f3565..e74551976 100644 --- a/js/src/forum/components/IndexPage.js +++ b/js/src/forum/components/IndexPage.js @@ -42,26 +42,16 @@ export default class IndexPage extends Page { app.history.push('index', app.translator.trans('core.forum.header.back_to_index_tooltip')); this.bodyClass = 'App--index'; - - this.currentPath = m.route.get(); } - onbeforeupdate(vnode) { - super.onbeforeupdate(vnode); + onNewRoute() { + super.onNewRoute(); - const curPath = m.route.get(); + app.discussions.clear(); - if (this.currentPath !== curPath) { - this.onNewRoute(); + app.discussions.refreshParams(app.search.params()); - app.discussions.clear(); - - app.discussions.refreshParams(app.search.params()); - - this.currentPath = curPath; - - this.setTitle(); - } + this.setTitle(); } view() { diff --git a/js/src/forum/components/UserPage.js b/js/src/forum/components/UserPage.js index 1171342f1..19a4d4dad 100644 --- a/js/src/forum/components/UserPage.js +++ b/js/src/forum/components/UserPage.js @@ -28,18 +28,13 @@ export default class UserPage extends Page { this.bodyClass = 'App--user'; - this.prevUsername = m.route.param('username'); + this.currUsername = m.route.param('username'); } - onbeforeupdate() { - const currUsername = m.route.param('username'); - if (currUsername !== this.prevUsername) { - this.onNewRoute(); + onNewRoute() { + super.onNewRoute(); - this.prevUsername = currUsername; - - this.loadUser(currUsername); - } + this.loadUser(m.route.param('username')); } view() {