Commit 502dc33c by Matthew Mongeau

Display comments and active thread.

parent bc3d753b
...@@ -6,22 +6,19 @@ class @DiscussionRouter extends Backbone.Router ...@@ -6,22 +6,19 @@ class @DiscussionRouter extends Backbone.Router
initialize: (options) -> initialize: (options) ->
@user = options['user'] @user = options['user']
@discussion = options['discussion'] @discussion = options['discussion']
@displayNav() @nav = new DiscussionThreadListView(collection: @discussion, el: $(".post-list"))
@forum = null @nav.on "thread:selected", @navigateToThread
@nav.render()
allThreads: -> allThreads: ->
true true
showThread: (forum_name, thread_id) -> showThread: (forum_name, thread_id) ->
@forum = forum_name @nav.setActiveThread(thread_id)
thread = @discussion.get(thread_id) thread = @discussion.get(thread_id)
view = new DiscussionThreadView(el: $(".discussion-column"), model: thread, user: @user) view = new DiscussionThreadView(el: $(".discussion-column"), model: thread, user: @user)
view.render() view.render()
displayNav: ->
view = new DiscussionThreadListView(collection: @discussion, el: $(".post-list"))
view.on "thread:selected", @navigateToThread
view.render()
navigateToThread: (thread_id) => navigateToThread: (thread_id) =>
@navigate("#{@forum}/threads/#{thread_id}", trigger: true) thread = @discussion.get(thread_id)
@navigate("#{thread.get("commentable_id")}/threads/#{thread_id}", trigger: true)
...@@ -9,4 +9,10 @@ class @DiscussionThreadListView extends Backbone.View ...@@ -9,4 +9,10 @@ class @DiscussionThreadListView extends Backbone.View
@$el.append(view.el) @$el.append(view.el)
threadSelected: (thread_id) => threadSelected: (thread_id) =>
@setActiveThread(thread_id)
@trigger("thread:selected", thread_id) @trigger("thread:selected", thread_id)
setActiveThread: (thread_id) ->
@$("a").removeClass("active")
@$("a[data-id='#{thread_id}']").addClass("active")
...@@ -18,9 +18,20 @@ class @DiscussionThreadView extends Backbone.View ...@@ -18,9 +18,20 @@ class @DiscussionThreadView extends Backbone.View
if @user.voted(@model) if @user.voted(@model)
@$(".vote-btn").addClass("is-cast") @$(".vote-btn").addClass("is-cast")
@$("span.timeago").timeago()
@renderResponses()
@ @
renderResponses: ->
$.ajax @model.id, success: (data, textStatus, xhr) =>
comments = new Comments(data['content']['children'])
comments.each @renderResponse
renderResponse: (response) =>
view = new ThreadResponseView(model: response)
view.render()
@$(".responses").append(view.el)
toggleVote: -> toggleVote: ->
@$(".vote-btn").toggleClass("is-cast") @$(".vote-btn").toggleClass("is-cast")
if @$(".vote-btn").hasClass("is-cast") if @$(".vote-btn").hasClass("is-cast")
......
class @ResponseCommentView extends Backbone.View
tagName: "li"
template: _.template($("#response-comment-template").html())
render: ->
@$el.html(@template(@model.toJSON()))
@
class @ThreadResponseView extends Backbone.View
tagName: "li"
template: _.template($("#thread-response-template").html())
render: ->
@$el.html(@template(@model.toJSON()))
@renderComments()
@
renderComments: ->
@model.get("comments").each @renderComment
renderComment: (comment) =>
view = new ResponseCommentView(model: comment)
view.render()
@$(".comments").append(view.el)
...@@ -30,4 +30,3 @@ ...@@ -30,4 +30,3 @@
</article> </article>
<%include file="_js_data.html" /> <%include file="_js_data.html" />
<script type="text/javascript">$(document).ready(function() { $("span.timeago").timeago() })</script>
...@@ -125,8 +125,18 @@ ...@@ -125,8 +125,18 @@
</article> </article>
</script> </script>
<script type="text/template" id="thread-response-template">
<div class="response-body">${"<%= body %>"}</div>
<ol class="comments">
</ol>
</script>
<script type="text/template" id="response-comment-template">
<p>${'<%= body %>'}</p>
</script>
<script type="text/template" id="thread-list-item-template"> <script type="text/template" id="thread-list-item-template">
<a href="${'<%= id %>'}"><span class="title">${"<%= title %>"}</span> <span class="comments-count">${"<%= comments_count %>"}</span><span class="votes-count">+${"<%= votes['up_count'] %>"}</span></a> <a href="${'<%= id %>'}" data-id="${'<%= id %>'}"><span class="title">${"<%= title %>"}</span> <span class="comments-count">${"<%= comments_count %>"}</span><span class="votes-count">+${"<%= votes['up_count'] %>"}</span></a>
</script> </script>
<script> <script>
......
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