Commit 8c7e56a3 by Matthew Mongeau

Fix voting.

parent 70eca949
...@@ -447,6 +447,14 @@ if Backbone? ...@@ -447,6 +447,14 @@ if Backbone?
@set('subscribed', false) @set('subscribed', false)
@trigger "thread:unfollow" @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: -> display_body: ->
if @has("highlighted_body") if @has("highlighted_body")
String(@get("highlighted_body")).replace(/<highlight>/g, '<mark>').replace(/<\/highlight>/g, '</mark>') String(@get("highlighted_body")).replace(/<highlight>/g, '<mark>').replace(/<\/highlight>/g, '</mark>')
......
...@@ -4,3 +4,11 @@ class @DiscussionUser extends Backbone.Model ...@@ -4,3 +4,11 @@ class @DiscussionUser extends Backbone.Model
voted: (thread) -> voted: (thread) ->
_.include(@get('upvoted_ids'), thread.id) _.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()
...@@ -22,22 +22,32 @@ class @DiscussionThreadView extends DiscussionContentView ...@@ -22,22 +22,32 @@ class @DiscussionThreadView extends DiscussionContentView
template: _.template($("#thread-template").html()) template: _.template($("#thread-template").html())
initialize: ->
@model.on "change", @updateModelDetails
render: -> render: ->
@$el.html(@template(@model.toJSON())) @$el.html(@template(@model.toJSON()))
@model.bind "change", @updateModelDetails @renderDogear()
if window.user.following(@model) @renderVoted()
@$(".dogear").addClass("is-followed")
if window.user.voted(@model)
@$(".vote-btn").addClass("is-cast")
@$("span.timeago").timeago() @$("span.timeago").timeago()
Markdown.makeWmdEditor @$(".reply-body"), "", DiscussionUtil.urlFor("upload"), (text) -> DiscussionUtil.postMathJaxProcessor(text) Markdown.makeWmdEditor @$(".reply-body"), "", DiscussionUtil.urlFor("upload"), (text) -> DiscussionUtil.postMathJaxProcessor(text)
@convertMath() @convertMath()
@renderResponses() @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: => 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: -> convertMath: ->
element = @$(".post-body") element = @$(".post-body")
...@@ -64,10 +74,10 @@ class @DiscussionThreadView extends DiscussionContentView ...@@ -64,10 +74,10 @@ class @DiscussionThreadView extends DiscussionContentView
toggleVote: (event) -> toggleVote: (event) ->
event.preventDefault() event.preventDefault()
if not @model.get('voted')#@$(".discussion-vote").hasClass("is-cast") if window.user.voted(@model)
@vote()
else
@unvote() @unvote()
else
@vote()
toggleFollowing: (event) -> toggleFollowing: (event) ->
$elem = $(event.target) $elem = $(event.target)
...@@ -84,9 +94,8 @@ class @DiscussionThreadView extends DiscussionContentView ...@@ -84,9 +94,8 @@ class @DiscussionThreadView extends DiscussionContentView
type: "POST" type: "POST"
vote: -> vote: ->
window.user.vote(@model)
url = @model.urlFor("upvote") 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 DiscussionUtil.safeAjax
$elem: @$(".discussion-vote") $elem: @$(".discussion-vote")
url: url url: url
...@@ -96,9 +105,8 @@ class @DiscussionThreadView extends DiscussionContentView ...@@ -96,9 +105,8 @@ class @DiscussionThreadView extends DiscussionContentView
@model.set(response) @model.set(response)
unvote: -> unvote: ->
window.user.unvote(@model)
url = @model.urlFor("unvote") 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 DiscussionUtil.safeAjax
$elem: @$(".discussion-vote") $elem: @$(".discussion-vote")
url: url url: url
......
...@@ -3,7 +3,7 @@ ...@@ -3,7 +3,7 @@
<a href="javascript:void(0)" class="dogear action-follow"></a> <a href="javascript:void(0)" class="dogear action-follow"></a>
<div class="discussion-post"> <div class="discussion-post">
<header> <header>
<a href="#" class="vote-btn discussion-vote discussion-vote-up"><span class="plus-icon">+</span> <span class='votes-count-number'>${'<%- votes["up_count"] %>'}</span></a> <a href="#" class="vote-btn discussion-vote discussion-vote-up" data-role="discussion-vote"><span class="plus-icon">+</span> <span class='votes-count-number'>${'<%- votes["up_count"] %>'}</span></a>
<h1>${'<%- title %>'}</h1> <h1>${'<%- title %>'}</h1>
<p class="posted-details"> <p class="posted-details">
<span class="timeago" title="${'<%- created_at %>'}">${'<%- created_at %>'}</span> by <span class="timeago" title="${'<%- created_at %>'}">${'<%- created_at %>'}</span> by
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment