-
-
Notifications
You must be signed in to change notification settings - Fork 834
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Abstract away onNewRoute and routeHasChanged #2359
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This line can be probably removed I think, since it's called in |
||
} | ||
|
||
routeHasChanged() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we want this to be public API, or could we just do the test in There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think public API is good: subclasses with more complex use cases (like DiscussionPage) could use it to do more of those checks, and also possibly handle some side effects if the route hasn't changed but we still want to do something (like scrolling to a post). |
||
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) { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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); | ||
|
||
if (m.route.get() !== this.prevRoute) { | ||
this.onNewRoute(); | ||
this.prevRoute = m.route.get(); | ||
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()) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd split this out in this PR already, since the diff is very hard to read. Something like this: isSameRoute() {
const idParam = m.route.param('id');
return this.discussion && idParam && idParam.split('-')[0] === this.discussion.id();
}
onSameRoute() {
const near = m.route.param('near') || '1';
if (near !== String(this.near)) {
this.stream.goToNumber(near);
}
this.near = near;
} and: onNewRoute() {
if (this.isSameRoute()) return this.onSameRoute();
super.onNewRoute();
....
} |
||
const near = m.route.param('near') || '1'; | ||
|
||
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(); | ||
} | ||
} | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd move that to the end of the function. This way everything in
oninit
would run beforeonNewRoute
.