diff --git a/lms/static/coffee/src/discussion/content.coffee b/lms/static/coffee/src/discussion/content.coffee index 7540b508ef..ee6c50ecf7 100644 --- a/lms/static/coffee/src/discussion/content.coffee +++ b/lms/static/coffee/src/discussion/content.coffee @@ -447,6 +447,14 @@ if Backbone? @set('subscribed', false) @trigger "thread:unfollow" + vote: -> + @get("votes")["up_count"] = parseInt(@get("votes")["up_count"]) + 1 + @trigger "change" + + unvote: -> + @get("votes")["up_count"] = parseInt(@get("votes")["up_count"]) - 1 + @trigger "change" + display_body: -> if @has("highlighted_body") String(@get("highlighted_body")).replace(//g, '').replace(/<\/highlight>/g, '') diff --git a/lms/static/coffee/src/discussion/models/discussion_user.coffee b/lms/static/coffee/src/discussion/models/discussion_user.coffee index 6cf0dbedbb..6449e80b85 100644 --- a/lms/static/coffee/src/discussion/models/discussion_user.coffee +++ b/lms/static/coffee/src/discussion/models/discussion_user.coffee @@ -4,3 +4,11 @@ class @DiscussionUser extends Backbone.Model voted: (thread) -> _.include(@get('upvoted_ids'), thread.id) + + vote: (thread) -> + @get('upvoted_ids').push(thread.id) + thread.vote() + + unvote: (thread) -> + @set('upvoted_ids', _.without(@get('upvoted_ids'), thread.id)) + thread.unvote() diff --git a/lms/static/coffee/src/discussion/views/discussion_thread_view.coffee b/lms/static/coffee/src/discussion/views/discussion_thread_view.coffee index 8834a38b77..514f60aef3 100644 --- a/lms/static/coffee/src/discussion/views/discussion_thread_view.coffee +++ b/lms/static/coffee/src/discussion/views/discussion_thread_view.coffee @@ -22,22 +22,32 @@ class @DiscussionThreadView extends DiscussionContentView template: _.template($("#thread-template").html()) + initialize: -> + @model.on "change", @updateModelDetails + render: -> @$el.html(@template(@model.toJSON())) - @model.bind "change", @updateModelDetails - if window.user.following(@model) - @$(".dogear").addClass("is-followed") - - if window.user.voted(@model) - @$(".vote-btn").addClass("is-cast") + @renderDogear() + @renderVoted() @$("span.timeago").timeago() Markdown.makeWmdEditor @$(".reply-body"), "", DiscussionUtil.urlFor("upload"), (text) -> DiscussionUtil.postMathJaxProcessor(text) @convertMath() @renderResponses() @ + renderDogear: -> + if window.user.following(@model) + @$(".dogear").addClass("is-followed") + + renderVoted: => + if window.user.voted(@model) + @$("[data-role=discussion-vote]").addClass("is-cast") + else + @$("[data-role=discussion-vote]").removeClass("is-cast") + updateModelDetails: => - @$(".discussion-vote .votes-count-number").html(@model.get("votes")["up_count"]) + @renderVoted() + @$("[data-role=discussion-vote] .votes-count-number").html(@model.get("votes")["up_count"]) convertMath: -> element = @$(".post-body") @@ -64,10 +74,10 @@ class @DiscussionThreadView extends DiscussionContentView toggleVote: (event) -> event.preventDefault() - if not @model.get('voted')#@$(".discussion-vote").hasClass("is-cast") - @vote() - else + if window.user.voted(@model) @unvote() + else + @vote() toggleFollowing: (event) -> $elem = $(event.target) @@ -84,9 +94,8 @@ class @DiscussionThreadView extends DiscussionContentView type: "POST" vote: -> + window.user.vote(@model) url = @model.urlFor("upvote") - @model.set('votes_point', parseInt(@model.get('votes_point')) + 1) - #@$(".discussion-vote .votes-count-number").html(parseInt(@$(".discussion-vote .votes-count-number").html()) + 1) DiscussionUtil.safeAjax $elem: @$(".discussion-vote") url: url @@ -96,9 +105,8 @@ class @DiscussionThreadView extends DiscussionContentView @model.set(response) unvote: -> + window.user.unvote(@model) url = @model.urlFor("unvote") - @model.set('votes_point', parseInt(@model.get('votes_point')) - 1) - #@$(".discussion-vote .votes-count-number").html(parseInt(@$(".discussion-vote .votes-count-number").html()) - 1) DiscussionUtil.safeAjax $elem: @$(".discussion-vote") url: url diff --git a/lms/templates/discussion/_underscore_templates.html b/lms/templates/discussion/_underscore_templates.html index 64fca600e4..1a5fe6158d 100644 --- a/lms/templates/discussion/_underscore_templates.html +++ b/lms/templates/discussion/_underscore_templates.html @@ -3,7 +3,7 @@
- + ${'<%- votes["up_count"] %>'} + + ${'<%- votes["up_count"] %>'}

${'<%- title %>'}

${'<%- created_at %>'} by