Refactor PostActionSummary a bit.

- Remove `Discourse` constant.
- Use `LikeCount` instead of `Likecount`
This commit is contained in:
Robin Ward 2015-07-10 14:32:14 -04:00
parent ce86c9956e
commit 284b86cf5d
4 changed files with 10 additions and 18 deletions

View File

@ -91,7 +91,7 @@ const PostMenuComponent = Ember.Component.extend(StringBuffer, {
action = $target.data('action') || $target.parent().data('action'); action = $target.data('action') || $target.parent().data('action');
if (!action) return; if (!action) return;
const handler = this["click" + action.replace(/[\+-]/, "").capitalize()]; const handler = this["click" + action.classify()];
if (!handler) return; if (!handler) return;
handler.call(this, this.get('post')); handler.call(this, this.get('post'));
@ -109,19 +109,6 @@ const PostMenuComponent = Ember.Component.extend(StringBuffer, {
return buffer.push(iconHTML(icon) + "</button>"); return buffer.push(iconHTML(icon) + "</button>");
}, },
renderLikes(post, buffer) {
const likeCount = this.get('likeAction.count') || 0;
if (likeCount === 0) { return; }
buffer.push("<button class='show-likes' data-action='likes'>");
buffer.push("<span class='badge-posts'>" + Discourse.Formatter.number(likeCount) + "</span>");
buffer.push(I18n.t("post.has_likes", { count: likeCount }));
const icon = (this.get('likeAction.users.length') > 0) ? 'chevron-up' : 'chevron-down';
return buffer.push(iconHTML(icon) + "</button>");
},
renderButtons(post, buffer) { renderButtons(post, buffer) {
const self = this; const self = this;
const allButtons = []; const allButtons = [];
@ -141,7 +128,7 @@ const PostMenuComponent = Ember.Component.extend(StringBuffer, {
const yours = post.get('yours'); const yours = post.get('yours');
this.siteSettings.post_menu.split("|").forEach(function(i) { this.siteSettings.post_menu.split("|").forEach(function(i) {
const creator = self["buttonFor" + i.replace(/[\+-]/, '').capitalize()]; const creator = self["buttonFor" + i.classify()];
if (creator) { if (creator) {
const button = creator.call(self, post); const button = creator.call(self, post);
if (button) { if (button) {
@ -177,7 +164,7 @@ const PostMenuComponent = Ember.Component.extend(StringBuffer, {
buffer.push("</div>"); buffer.push("</div>");
}, },
clickLikecount() { clickLikeCount() {
const likeAction = this.get('post.actionByName.like'); const likeAction = this.get('post.actionByName.like');
if (likeAction) { if (likeAction) {
const users = likeAction.get('users'); const users = likeAction.get('users');
@ -259,7 +246,7 @@ const PostMenuComponent = Ember.Component.extend(StringBuffer, {
} }
}, },
buttonForLikecount() { buttonForLikeCount() {
var likeCount = this.get('post.like_count') || 0; var likeCount = this.get('post.like_count') || 0;
if (likeCount > 0) { if (likeCount > 0) {
const likedPost = !!this.get('likeAction.acted'); const likedPost = !!this.get('likeAction.acted');

View File

@ -56,6 +56,7 @@ export default RestModel.extend({
// Perform this action // Perform this action
act: function(post, opts) { act: function(post, opts) {
if (!opts) opts = {}; if (!opts) opts = {};
const action = this.get('actionType.name_key'); const action = this.get('actionType.name_key');

View File

@ -1,5 +1,6 @@
import RestModel from 'discourse/models/rest'; import RestModel from 'discourse/models/rest';
import { popupAjaxError } from 'discourse/lib/ajax-error'; import { popupAjaxError } from 'discourse/lib/ajax-error';
import ActionSummary from 'discourse/models/action-summary';
const Post = RestModel.extend({ const Post = RestModel.extend({
@ -359,13 +360,15 @@ Post.reopenClass({
munge(json) { munge(json) {
if (json.actions_summary) { if (json.actions_summary) {
const lookup = Em.Object.create(); const lookup = Em.Object.create();
// this area should be optimized, it is creating way too many objects per post // this area should be optimized, it is creating way too many objects per post
json.actions_summary = json.actions_summary.map(function(a) { json.actions_summary = json.actions_summary.map(function(a) {
a.actionType = Discourse.Site.current().postActionTypeById(a.id); a.actionType = Discourse.Site.current().postActionTypeById(a.id);
const actionSummary = Discourse.ActionSummary.create(a); const actionSummary = ActionSummary.create(a);
lookup[a.actionType.name_key] = actionSummary; lookup[a.actionType.name_key] = actionSummary;
return actionSummary; return actionSummary;
}); });
json.actionByName = lookup; json.actionByName = lookup;
} }

View File

@ -28,6 +28,7 @@
//= require ./discourse/models/rest //= require ./discourse/models/rest
//= require ./discourse/models/model //= require ./discourse/models/model
//= require ./discourse/models/post-action-type //= require ./discourse/models/post-action-type
//= require ./discourse/models/action-summary
//= require ./discourse/models/post //= require ./discourse/models/post
//= require ./discourse/models/post-stream //= require ./discourse/models/post-stream
//= require ./discourse/models/topic-details //= require ./discourse/models/topic-details