Commit a905ac9e by Rocky Duan

updated backbone & single thread retrieval now working

parent d189a49e
......@@ -10,6 +10,7 @@ from mitxmako.shortcuts import render_to_response, render_to_string
from courseware.courses import get_course_with_access
from urllib import urlencode
from operator import methodcaller
from django_comment_client.permissions import check_permissions_by_view
from django_comment_client.utils import merge_dict, extract, strip_none
......@@ -188,6 +189,7 @@ def single_thread(request, course_id, discussion_id, thread_id):
return utils.JsonResponse({
'html': html,
'content': thread.to_dict(),
'annotated_content_info': annotated_content_info,
})
......
......@@ -15,6 +15,7 @@ TEMPLATE_DEBUG = True
MITX_FEATURES['DISABLE_START_DATES'] = True
MITX_FEATURES['ENABLE_SQL_TRACKING_LOGS'] = True
MITX_FEATURES['ENABLE_DISCUSSION_SERVICE'] = True
WIKI_ENABLED = True
......
......@@ -18,12 +18,61 @@ class @Content extends Backbone.Model
can: (action) ->
DiscussionUtil.getContentInfo @id, action
initialize: ->
@set('comments', new Comments())
if @get('children')
@get('comments').reset @get('children'), {silent: false}
class @ContentView extends Backbone.View
$: (selector) ->
@$local.find(selector)
showSingleThread: (event) ->
$threadTitle = @$(".thread-title")
$showComments = @$(".discussion-show-comments")
if not $showComments.hasClass("first-time") and (not $showComments.length or not $threadTitle.length)
return
rebindHideEvents = ->
$threadTitle.unbind('click').click @hideSingleThread
$showComments.unbind('click').click @hideSingleThread
$showComments.removeClass("discussion-show-comments")
.addClass("discussion-hide-comments")
prevHtml = $showComments.html()
$showComments.html prevHtml.replace "Show", "Hide"
if not $showComments.hasClass("first-time") and @$el.children(".comments").length
@$el.children(".comments").show()
rebindHideEvents()
else
discussion_id = @model.discussion.id
url = DiscussionUtil.urlFor('retrieve_single_thread', discussion_id, @model.id)
DiscussionUtil.safeAjax
$elem: $.merge($threadTitle, $showComments)
url: url
type: "GET"
dataType: 'json'
success: (response, textStatus) =>
DiscussionUtil.bulkExtendContentInfo response['annotated_content_info']
@$el.append(response['html'])
@model.get('comments').reset response.content.children, {silent: false}
@initCommentViews()
$showComments.removeClass("first-time")
rebindHideEvents()
initCommentViews: ->
@$el.children(".comments").children(".comment").each (index, elem) =>
model = @model.get('comments').find $(elem).attr("_id")
if not model.view
commentView = new CommentView el: elem, model: model
hideSingleThread: ->
reply: ->
cancelReply: ->
unvote: (event) ->
url = DiscussionUtil.urlFor("undo_vote_for_#{@model.get('type')}", @model.id)
......@@ -55,12 +104,6 @@ class @ContentView extends Backbone.View
@$(".discussion-vote-#{value}").addClass("voted")
@$(".discussion-votes-point").html response.votes.point
hideSingleThread: ->
reply: ->
cancelReply: ->
endorse: ->
close: ->
......@@ -85,6 +128,7 @@ class @ContentView extends Backbone.View
initLocal: ->
@$local = @$el.children(".local")
@$delegateElement = @$local
initFollowThread: ->
$el.children(".discussion-content")
......@@ -117,6 +161,7 @@ class @ContentView extends Backbone.View
@initTimeago()
@initBody()
@initActions()
@initCommentViews()
class @Thread extends @Content
......@@ -124,5 +169,16 @@ class @ThreadView extends @ContentView
class @Comment extends @Content
class @CommentView extends @ContentView
class @Comments extends Backbone.Collection
model: Comment
initialize: ->
@bind "add", (item) =>
item.collection = @
find: (id) ->
_.first @where(id: id)
class @Discussion extends Backbone.Collection
model: Thread
initialize: ->
this.bind "add", (item) =>
item.collection = this
@bind "add", (item) =>
console.log item
item.discussion = @
find: (id) ->
_.first @where(id: id)
......@@ -16,9 +18,11 @@ class @DiscussionView extends Backbone.View
initLocal: ->
@$local = @$el.children(".local")
@$delegateElement = @$local
initialize: ->
@initLocal()
@model.id = @$el.attr("_id")
@model.view = @
@$el.children(".threads").children(".thread").each (index, elem) =>
threadView = new ThreadView el: elem, model: @model.find $(elem).attr("_id")
......
......@@ -5,5 +5,6 @@ $ ->
$("section.discussion").each (index, elem) ->
discussionData = DiscussionUtil.getDiscussionData(elem)
discussion = new Discussion(discussionData)
discussion = new Discussion()
discussion.reset(discussionData, {silent: false})
view = new DiscussionView(el: elem, model: discussion)
......@@ -5,7 +5,7 @@
</%def>
<%def name="render_content_with_comments(content)">
<div class="${content['type']}" _id="${content['id']}" _discussion_id="${content.get('commentable_id')}" _author_id="${helpers.show_if(content['user_id'], content.get('anonymous'))}">
<div class="${content['type']}${helpers.show_if(' endorsed', content.get('endorsed'))}" _id="${content['id']}" _discussion_id="${content.get('commentable_id', '')}" _author_id="${helpers.show_if(content['user_id'], not content.get('anonymous'))}">
<div class="local">${render_content(content)}</div>
% if content.get('children') is not None:
${render_comments(content['children'])}
......
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