Skip to content

Commit

Permalink
Add init hook as a way to effectively monkey patch constructors
Browse files Browse the repository at this point in the history
Related to #246
  • Loading branch information
tobyzerner committed Sep 8, 2015
1 parent 1aaff46 commit e862163
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 13 deletions.
6 changes: 3 additions & 3 deletions js/forum/src/components/DiscussionPage.js
Expand Up @@ -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();
Expand All @@ -156,7 +156,7 @@ export default class DiscussionPage extends Page {
*
* @param {Discussion} discussion
*/
init(discussion) {
show(discussion) {
this.discussion = discussion;

app.setTitle(discussion.title());
Expand Down
8 changes: 4 additions & 4 deletions js/forum/src/components/PostStream.js
Expand Up @@ -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);
}

/**
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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));
}

/**
Expand All @@ -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));
}

/**
Expand Down
4 changes: 2 additions & 2 deletions js/forum/src/components/PostsUserPage.js
Expand Up @@ -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();
}
Expand Down
2 changes: 1 addition & 1 deletion js/forum/src/components/SettingsPage.js
Expand Up @@ -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'));
}

Expand Down
6 changes: 3 additions & 3 deletions js/forum/src/components/UserPage.js
Expand Up @@ -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());
Expand All @@ -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));
}
}

Expand Down
10 changes: 10 additions & 0 deletions js/lib/Component.js
Expand Up @@ -53,6 +53,16 @@ export default class Component {
* @public
*/
this.element = null;

this.init();
}

/**
* Called when the component is constructed.
*
* @protected
*/
init() {
}

/**
Expand Down

0 comments on commit e862163

Please sign in to comment.