Commit e0f39bdd by Rocky Duan

some refactoring on error handling

parent 1aa0cbd8
...@@ -28,9 +28,10 @@ Discussion = @Discussion ...@@ -28,9 +28,10 @@ Discussion = @Discussion
if $replyView.length if $replyView.length
$replyView.show() $replyView.show()
else else
thread_id = $discussionContent.parents(".thread").attr("_id")
view = { view = {
id: id id: id
showWatchCheckbox: $discussionContent.parents(".thread").attr("_id") not in $$user_info.subscribed_thread_ids showWatchCheckbox: not Discussion.isSubscribed(thread_id, "thread")
} }
$discussionContent.append Mustache.render Discussion.replyTemplate, view $discussionContent.append Mustache.render Discussion.replyTemplate, view
Markdown.makeWmdEditor $local(".reply-body"), "-reply-body-#{id}", Discussion.urlFor('upload') Markdown.makeWmdEditor $local(".reply-body"), "-reply-body-#{id}", Discussion.urlFor('upload')
...@@ -61,19 +62,15 @@ Discussion = @Discussion ...@@ -61,19 +62,15 @@ Discussion = @Discussion
autowatch = false || $local(".discussion-auto-watch").is(":checked") autowatch = false || $local(".discussion-auto-watch").is(":checked")
Discussion.safeAjax Discussion.safeAjax
$elem: $(elem)
url: url url: url
type: "POST" type: "POST"
data: data:
body: body body: body
anonymous: anonymous anonymous: anonymous
autowatch: autowatch autowatch: autowatch
success: (response, textStatus) -> success: Discussion.formErrorHandler $local(".discussion-errors"), (response, textStatus) ->
if response.errors? and response.errors.length > 0 Discussion.handleAnchorAndReload(response)
errorsField = $local(".discussion-errors").empty()
for error in response.errors
errorsField.append($("<li>").addClass("new-post-form-error").html(error))
else
Discussion.handleAnchorAndReload(response)
dataType: 'json' dataType: 'json'
handleVote = (elem, value) -> handleVote = (elem, value) ->
...@@ -119,14 +116,13 @@ Discussion = @Discussion ...@@ -119,14 +116,13 @@ Discussion = @Discussion
title = $local(".thread-title-edit").val() title = $local(".thread-title-edit").val()
body = $local("#wmd-input-thread-body-edit-#{id}").val() body = $local("#wmd-input-thread-body-edit-#{id}").val()
tags = $local(".thread-tags-edit").val() tags = $local(".thread-tags-edit").val()
$.post url, {title: title, body: body, tags: tags}, (response, textStatus) -> $.ajax
if response.errors url: url
errorsField = $local(".discussion-update-errors").empty() type: "POST"
for error in response.errors data: {title: title, body: body, tags: tags},
errorsField.append($("<li>").addClass("new-post-form-error").html(error)) success: Discussion.formErrorHandler $local(".discussion-update-errors"), (response, textStatus) ->
else
Discussion.handleAnchorAndReload(response) Discussion.handleAnchorAndReload(response)
, 'json' dataType: 'json'
handleEditComment = (elem) -> handleEditComment = (elem) ->
$local(".discussion-content-wrapper").hide() $local(".discussion-content-wrapper").hide()
...@@ -134,10 +130,7 @@ Discussion = @Discussion ...@@ -134,10 +130,7 @@ Discussion = @Discussion
if $editView.length if $editView.length
$editView.show() $editView.show()
else else
view = { view = { id: id, body: $local(".comment-raw-body").html() }
id: id
body: $local(".comment-raw-body").html()
}
$discussionContent.append Mustache.render Discussion.editCommentTemplate, view $discussionContent.append Mustache.render Discussion.editCommentTemplate, view
Markdown.makeWmdEditor $local(".comment-body-edit"), "-comment-body-edit-#{id}", Discussion.urlFor('update_comment', id) Markdown.makeWmdEditor $local(".comment-body-edit"), "-comment-body-edit-#{id}", Discussion.urlFor('update_comment', id)
$local(".discussion-submit-update").unbind("click").click -> handleSubmitEditComment(this) $local(".discussion-submit-update").unbind("click").click -> handleSubmitEditComment(this)
...@@ -146,14 +139,13 @@ Discussion = @Discussion ...@@ -146,14 +139,13 @@ Discussion = @Discussion
handleSubmitEditComment= (elem) -> handleSubmitEditComment= (elem) ->
url = Discussion.urlFor('update_comment', id) url = Discussion.urlFor('update_comment', id)
body = $local("#wmd-input-comment-body-edit-#{id}").val() body = $local("#wmd-input-comment-body-edit-#{id}").val()
$.post url, {body: body}, (response, textStatus) -> $.ajax
if response.errors url: url
errorsField = $local(".discussion-update-errors").empty() data: {body: body}
for error in response.errors type: "POST"
errorsField.append($("<li>").addClass("new-post-form-error").html(error)) success: Discussion.formErrorHandler $local(".discussion-update-errors"), (response, textStatus) ->
else
Discussion.handleAnchorAndReload(response) Discussion.handleAnchorAndReload(response)
, 'json' dataType: "json"
handleEndorse = (elem) -> handleEndorse = (elem) ->
url = Discussion.urlFor('endorse_comment', id) url = Discussion.urlFor('endorse_comment', id)
...@@ -203,35 +195,38 @@ Discussion = @Discussion ...@@ -203,35 +195,38 @@ Discussion = @Discussion
rebindHideEvents() rebindHideEvents()
dataType: 'json' dataType: 'json'
Discussion.bindLocalEvents $local,
$local(".thread-title").click handleShowSingleThread
$local(".discussion-show-comments").click handleShowSingleThread "click .thread-title": ->
handleShowSingleThread(this)
$local(".discussion-reply-thread").click -> "click .discussion-show-comments": ->
handleShowSingleThread($local(".thread-title")) handleShowSingleThread(this)
handleReply(this)
$local(".discussion-reply-comment").click -> "click .discussion-reply-thread": ->
handleReply(this) handleShowSingleThread($local(".thread-title"))
handleReply(this)
$local(".discussion-cancel-reply").click -> "click .discussion-reply-comment": ->
handleCancelReply(this) handleReply(this)
$local(".discussion-vote-up").click -> "click .discussion-cancel-reply": ->
handleVote(this, "up") handleCancelReply(this)
$local(".discussion-vote-down").click -> "click .discussion-vote-up": ->
handleVote(this, "down") handleVote(this, "up")
$local(".discussion-endorse").click -> "click .discussion-vote-down": ->
handleEndorse(this) handleVote(this, "down")
$local(".discussion-edit").click -> "click .discussion-endorse": ->
if $content.hasClass("thread") handleEndorse(this)
handleEditThread(this)
else "click .discussion-edit": ->
handleEditComment(this) if $content.hasClass("thread")
handleEditThread(this)
else
handleEditComment(this)
initializeContent: (content) -> initializeContent: (content) ->
$content = $(content) $content = $(content)
......
...@@ -128,8 +128,6 @@ initializeFollowThread = (index, thread) -> ...@@ -128,8 +128,6 @@ initializeFollowThread = (index, thread) ->
$(elem).hide() $(elem).hide()
handleUpdateDiscussionContent = ($elem, $discussion, url) ->
handleAjaxSearch = (elem) -> handleAjaxSearch = (elem) ->
handle handle
$elem = $(elem) $elem = $(elem)
......
...@@ -65,3 +65,20 @@ Discussion = @Discussion ...@@ -65,3 +65,20 @@ Discussion = @Discussion
height: "30px" height: "30px"
width: "100%" width: "100%"
removeWithBackspace: true removeWithBackspace: true
isSubscribed: (id, type) ->
if type == "thread"
id in $$user_info.subscribed_thread_ids
else if type == "commentable" or type == "discussion"
id in $$user_info.subscribed_commentable_ids
else
id in $$user_info.subscribed_user_ids
formErrorHandler: (errorsField, success) ->
(response, textStatus, xhr) ->
if response.errors? and response.errors.length > 0
errorsField.empty()
for error in response.errors
errorsField.append($("<li>").addClass("new-post-form-error").html(error))
else
success(response, textStatus, xhr)
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