Merge branch 'feature/tomg/new-discussions' of github.com:MITx/mitx into feature/tomg/new-discussions

This commit is contained in:
Tom Giannattasio
2012-08-29 16:22:21 -04:00
10 changed files with 118 additions and 24 deletions

View File

@@ -0,0 +1,23 @@
class @DiscussionRouter extends Backbone.Router
routes:
"": "allThreads"
":forum_name/threads/:thread_id" : "showThread"
initialize: (options) ->
@discussion = options['discussion']
@nav = new DiscussionThreadListView(collection: @discussion, el: $(".post-list"))
@nav.on "thread:selected", @navigateToThread
@nav.render()
allThreads: ->
true
showThread: (forum_name, thread_id) ->
@nav.setActiveThread(thread_id)
thread = @discussion.get(thread_id)
view = new DiscussionThreadView(el: $(".discussion-column"), model: thread)
view.render()
navigateToThread: (thread_id) =>
thread = @discussion.get(thread_id)
@navigate("#{thread.get("commentable_id")}/threads/#{thread_id}", trigger: true)

View File

@@ -3,8 +3,7 @@ class @DiscussionUser
@content_info = content_info
following: (thread) ->
@content_info[thread.id]['subscribed'] == true
_.include(@content_info['subscribed_thread_ids'], thread.id)
voted: (thread) ->
@content_info[thread.id]['voted'] == 'up'
_.include(@content_info['upvoted_ids'], thread.id)

View File

@@ -4,5 +4,15 @@ class @DiscussionThreadListView extends Backbone.View
@
renderThreadListItem: (thread) =>
view = new ThreadListItemView(model: thread)
view.on "thread:selected", @threadSelected
view.render()
@$el.append(view.el)
threadSelected: (thread_id) =>
@setActiveThread(thread_id)
@trigger("thread:selected", thread_id)
setActiveThread: (thread_id) ->
@$("a").removeClass("active")
@$("a[data-id='#{thread_id}']").addClass("active")

View File

@@ -2,22 +2,35 @@ class @DiscussionThreadView extends Backbone.View
events:
"click .discussion-vote-up": "toggleVote"
"click .dogear": "toggleFollowing"
template: _.template($("#thread-template").html())
initialize: (options) ->
@user = options['user']
@model.bind "change", @updateModelDetails
@$el.html(@template(@model.toJSON()))
updateModelDetails: =>
@$(".votes-count-number").html(@model.get("votes")["up_count"])
render: ->
if @user.following(@model)
if window.user.following(@model)
@$(".dogear").addClass("is-followed")
if @user.voted(@model)
if window.user.voted(@model)
@$(".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: ->
@$(".vote-btn").toggleClass("is-cast")
if @$(".vote-btn").hasClass("is-cast")

View File

@@ -0,0 +1,6 @@
class @ResponseCommentView extends Backbone.View
tagName: "li"
template: _.template($("#response-comment-template").html())
render: ->
@$el.html(@template(@model.toJSON()))
@

View File

@@ -1,8 +1,15 @@
class @ThreadListItemView extends Backbone.View
tagName: "li"
template: _.template($("#thread-list-item-template").html())
events:
"click a": "threadSelected"
initialize: ->
@model.on "change", @render
render: =>
@$el.html(@template(@model.toJSON()))
if window.user.following(@model)
@$("a").addClass("followed")
@
threadSelected: ->
@trigger("thread:selected", @model.id)
false

View File

@@ -0,0 +1,15 @@
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)