Commit 7c9fcfbc by Rocky Duan

ajax submit new post

parent 4ceee380
...@@ -65,7 +65,9 @@ def create_thread(request, course_id, commentable_id): ...@@ -65,7 +65,9 @@ def create_thread(request, course_id, commentable_id):
'thread': response, 'thread': response,
} }
html = render_to_string('discussion/ajax_thread_only.html', context) html = render_to_string('discussion/ajax_thread_only.html', context)
return HtmlResponse(html) return JsonResponse({
'html': html,
})
else: else:
return JsonResponse(response) return JsonResponse(response)
......
...@@ -66,6 +66,7 @@ Discussion = @Discussion ...@@ -66,6 +66,7 @@ Discussion = @Discussion
$elem: $(elem) $elem: $(elem)
url: url url: url
type: "POST" type: "POST"
dataType: 'json'
data: data:
body: body body: body
anonymous: anonymous anonymous: anonymous
...@@ -80,7 +81,6 @@ Discussion = @Discussion ...@@ -80,7 +81,6 @@ Discussion = @Discussion
$local(".discussion-reply-new").hide() $local(".discussion-reply-new").hide()
$discussionContent.attr("status", "normal") $discussionContent.attr("status", "normal")
) )
dataType: 'json'
handleVote = (elem, value) -> handleVote = (elem, value) ->
contentType = if $content.hasClass("thread") then "thread" else "comment" contentType = if $content.hasClass("thread") then "thread" else "comment"
...@@ -128,10 +128,11 @@ Discussion = @Discussion ...@@ -128,10 +128,11 @@ Discussion = @Discussion
$.ajax $.ajax
url: url url: url
type: "POST" type: "POST"
dataType: 'json'
data: {title: title, body: body, tags: tags}, data: {title: title, body: body, tags: tags},
success: Discussion.formErrorHandler $local(".discussion-update-errors"), (response, textStatus) -> success: Discussion.formErrorHandler($local(".discussion-update-errors"), (response, textStatus) ->
Discussion.handleAnchorAndReload(response) Discussion.handleAnchorAndReload(response)
dataType: 'json' )
handleEditComment = (elem) -> handleEditComment = (elem) ->
$local(".discussion-content-wrapper").hide() $local(".discussion-content-wrapper").hide()
...@@ -150,11 +151,12 @@ Discussion = @Discussion ...@@ -150,11 +151,12 @@ Discussion = @Discussion
body = Discussion.getWmdContent $content, $local, "comment-body-edit" body = Discussion.getWmdContent $content, $local, "comment-body-edit"
$.ajax $.ajax
url: url url: url
data: {body: body}
type: "POST" type: "POST"
success: Discussion.formErrorHandler $local(".discussion-update-errors"), (response, textStatus) ->
Discussion.handleAnchorAndReload(response)
dataType: "json" dataType: "json"
data: {body: body}
success: Discussion.formErrorHandler($local(".discussion-update-errors"), (response, textStatus) ->
Discussion.handleAnchorAndReload(response)
)
handleEndorse = (elem) -> handleEndorse = (elem) ->
url = Discussion.urlFor('endorse_comment', id) url = Discussion.urlFor('endorse_comment', id)
...@@ -196,6 +198,7 @@ Discussion = @Discussion ...@@ -196,6 +198,7 @@ Discussion = @Discussion
$elem: $.merge($threadTitle, $showComments) $elem: $.merge($threadTitle, $showComments)
url: url url: url
type: "GET" type: "GET"
dataType: 'json'
success: (response, textStatus) -> success: (response, textStatus) ->
if not $$annotated_content_info? if not $$annotated_content_info?
window.$$annotated_content_info = {} window.$$annotated_content_info = {}
...@@ -205,7 +208,6 @@ Discussion = @Discussion ...@@ -205,7 +208,6 @@ Discussion = @Discussion
Discussion.initializeContent(comment) Discussion.initializeContent(comment)
Discussion.bindContentEvents(comment) Discussion.bindContentEvents(comment)
rebindHideEvents() rebindHideEvents()
dataType: 'json'
Discussion.bindLocalEvents $local, Discussion.bindLocalEvents $local,
......
...@@ -94,14 +94,25 @@ initializeFollowThread = (index, thread) -> ...@@ -94,14 +94,25 @@ initializeFollowThread = (index, thread) ->
body = Discussion.getWmdContent $discussion, $local, "new-post-body" body = Discussion.getWmdContent $discussion, $local, "new-post-body"
tags = $local(".new-post-tags").val() tags = $local(".new-post-tags").val()
url = Discussion.urlFor('create_thread', $local(".new-post-form").attr("_id")) url = Discussion.urlFor('create_thread', $local(".new-post-form").attr("_id"))
$.post url, {title: title, body: body, tags: tags}, (response, textStatus) -> Discussion.safeAjax
if response.errors $elem: $(elem)
errorsField = $local(".discussion-errors").empty() url: url
for error in response.errors type: "POST"
errorsField.append($("<li>").addClass("new-post-form-error").html(error)) dataType: 'json'
else data:
Discussion.handleAnchorAndReload(response) title: title
, 'json' body: body
tags: tags
success: Discussion.formErrorHandler($local(".new-post-form-error"), (response, textStatus) ->
console.log response
$thread = $(response.html)
$discussion.children(".threads").prepend($thread)
Discussion.setWmdContent $discussion, $local, "new-post-body", ""
Discussion.initializeContent($thread)
Discussion.bindContentEvents($thread)
$(".new-post-form").hide()
$local(".discussion-new-post").show()
)
handleCancelNewPost = (elem) -> handleCancelNewPost = (elem) ->
$local(".new-post-form").hide() $local(".new-post-form").hide()
......
...@@ -15,9 +15,11 @@ ...@@ -15,9 +15,11 @@
<div class="discussion-new-post control-button" href="javascript:void(0)">New Post</div> <div class="discussion-new-post control-button" href="javascript:void(0)">New Post</div>
</div> </div>
<%include file="_sort.html" /> <%include file="_sort.html" />
<div class="threads">
% for thread in threads: % for thread in threads:
${renderer.render_thread(course_id, thread, edit_thread=False, show_comments=False)} ${renderer.render_thread(course_id, thread, edit_thread=False, show_comments=False)}
% endfor % endfor
</div>
<%include file="_paginator.html" /> <%include file="_paginator.html" />
</section> </section>
......
...@@ -8,9 +8,11 @@ ...@@ -8,9 +8,11 @@
<div class="discussion-new-post control-button" href="javascript:void(0)">New Post</div> <div class="discussion-new-post control-button" href="javascript:void(0)">New Post</div>
<%include file="_sort.html" /> <%include file="_sort.html" />
</div> </div>
<div class="threads">
% for thread in threads: % for thread in threads:
${renderer.render_thread(course_id, thread, edit_thread=False, show_comments=False)} ${renderer.render_thread(course_id, thread, edit_thread=False, show_comments=False)}
% endfor % endfor
</div>
<%include file="_paginator.html" /> <%include file="_paginator.html" />
</section> </section>
......
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