Add init hook as a way to effectively monkey patch constructors

Related to #246
This commit is contained in:
Toby Zerner 2015-09-08 10:27:02 +09:30
parent 54554578a3
commit 76d0e7770c
6 changed files with 23 additions and 13 deletions

View File

@ -128,12 +128,12 @@ export default class DiscussionPage extends Page {
// component for the first time on page load, then any calls to m.redraw
// will be ineffective and thus any configs (scroll code) will be run
// before stuff is drawn to the page.
setTimeout(this.init.bind(this, preloadedDiscussion));
setTimeout(this.show.bind(this, preloadedDiscussion));
} else {
const params = this.requestParams();
app.store.find('discussions', m.route.param('id').split('-')[0], params)
.then(this.init.bind(this));
.then(this.show.bind(this));
}
m.lazyRedraw();
@ -156,7 +156,7 @@ export default class DiscussionPage extends Page {
*
* @param {Discussion} discussion
*/
init(discussion) {
show(discussion) {
this.discussion = discussion;
app.setTitle(discussion.title());

View File

@ -38,7 +38,7 @@ class PostStream extends mixin(Component, evented) {
this.loadPageTimeouts = {};
this.pagesLoading = 0;
this.init(this.props.includedPosts);
this.show(this.props.includedPosts);
}
/**
@ -153,7 +153,7 @@ class PostStream extends mixin(Component, evented) {
*
* @param {Post[]} posts
*/
init(posts) {
show(posts) {
this.visibleStart = posts.length ? this.discussion.postIds().indexOf(posts[0].id()) : 0;
this.visibleEnd = this.visibleStart + posts.length;
}
@ -421,7 +421,7 @@ class PostStream extends mixin(Component, evented) {
return app.store.find('posts', {
filter: {discussion: this.discussion.id()},
page: {near: number}
}).then(this.init.bind(this));
}).then(this.show.bind(this));
}
/**
@ -442,7 +442,7 @@ class PostStream extends mixin(Component, evented) {
this.reset(start, end);
return this.loadRange(start, end).then(this.init.bind(this));
return this.loadRange(start, end).then(this.show.bind(this));
}
/**

View File

@ -80,8 +80,8 @@ export default class PostsUserPage extends UserPage {
* Initialize the component with a user, and trigger the loading of their
* activity feed.
*/
init(user) {
super.init(user);
show(user) {
super.show(user);
this.refresh();
}

View File

@ -16,7 +16,7 @@ export default class SettingsPage extends UserPage {
constructor(...args) {
super(...args);
this.init(app.session.user);
this.show(app.session.user);
app.setTitle(app.trans('core.settings'));
}

View File

@ -71,7 +71,7 @@ export default class UserPage extends Page {
* @param {User} user
* @protected
*/
init(user) {
show(user) {
this.user = user;
app.setTitle(user.username());
@ -90,13 +90,13 @@ export default class UserPage extends Page {
app.store.all('users').some(user => {
if (user.username().toLowerCase() === lowercaseUsername && user.joinTime()) {
this.init(user);
this.show(user);
return true;
}
});
if (!this.user) {
app.store.find('users', username).then(this.init.bind(this));
app.store.find('users', username).then(this.show.bind(this));
}
}

View File

@ -53,6 +53,16 @@ export default class Component {
* @public
*/
this.element = null;
this.init();
}
/**
* Called when the component is constructed.
*
* @protected
*/
init() {
}
/**