Commit 3a254380 by Matthew Mongeau

Hook up comment counts

parent f683d375
class @DiscussionUser
constructor: (content_info) ->
@content_info = content_info
class @DiscussionUser extends Backbone.Model
following: (thread) ->
_.include(@content_info['subscribed_thread_ids'], thread.id)
_.include(@get('subscribed_thread_ids'), thread.id)
voted: (thread) ->
_.include(@content_info['upvoted_ids'], thread.id)
_.include(@get('upvoted_ids'), thread.id)
......@@ -28,9 +28,13 @@ class @DiscussionThreadView extends Backbone.View
renderResponse: (response) =>
view = new ThreadResponseView(model: response)
view.on "comment:add", @addComment
view.render()
@$(".responses").append(view.el)
addComment: =>
@model.trigger "comment:add"
toggleVote: ->
@$(".vote-btn").toggleClass("is-cast")
if @$(".vote-btn").hasClass("is-cast")
......
......@@ -7,6 +7,7 @@ class @ThreadListItemView extends Backbone.View
@model.on "change", @render
@model.on "thread:follow", @follow
@model.on "thread:unfollow", @unfollow
@model.on "comment:add", @addComment
render: =>
@$el.html(@template(@model.toJSON()))
if window.user.following(@model)
......@@ -19,3 +20,5 @@ class @ThreadListItemView extends Backbone.View
@$("a").addClass("followed")
unfollow: =>
@$("a").removeClass("followed")
addComment: =>
@$(".comments-count").html(parseInt(@$(".comments-count").html()) + 1)
......@@ -3,6 +3,8 @@ class @ThreadResponseView extends Backbone.View
template: _.template($("#thread-response-template").html())
events:
"click .vote-btn": "toggleVote"
"submit form": "submitComment"
render: ->
@$el.html(@template(@model.toJSON()))
if window.user.voted(@model)
......@@ -48,3 +50,19 @@ class @ThreadResponseView extends Backbone.View
success: (response, textStatus) =>
if textStatus == 'success'
@model.set(response)
submitComment: ->
url = @model.urlFor('reply')
body = @$(".comment-form-input").val()
comment = new Comment(body: body, created_at: (new Date()).toISOString(), username: window.user.get("username"))
@renderComment(comment)
@trigger "comment:add"
DiscussionUtil.safeAjax
$elem: $(event.target)
url: url
type: "POST"
dataType: 'json'
data:
body: body
false
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