Commit 5e091354 by Matthew Mongeau Committed by Ibrahim Awwal

Add backbone checks.

parent d0c2d315
class @DiscussionRouter extends Backbone.Router if Backbone?
routes: class @DiscussionRouter extends Backbone.Router
"": "allThreads" routes:
":forum_name/threads/:thread_id" : "showThread" "": "allThreads"
":forum_name/threads/:thread_id" : "showThread"
initialize: (options) ->
@discussion = options['discussion'] initialize: (options) ->
@nav = new DiscussionThreadListView(collection: @discussion, el: $(".sidebar")) @discussion = options['discussion']
@nav.on "thread:selected", @navigateToThread @nav = new DiscussionThreadListView(collection: @discussion, el: $(".sidebar"))
@nav.on "thread:removed", @navigateToAllThreads @nav.on "thread:selected", @navigateToThread
@nav.on "threads:rendered", @setActiveThread @nav.on "thread:removed", @navigateToAllThreads
@nav.render() @nav.on "threads:rendered", @setActiveThread
@nav.render()
@newPostView = new NewPostView(el: $(".new-post-article"), collection: @discussion)
@newPostView.on "thread:created", @navigateToThread @newPostView = new NewPostView(el: $(".new-post-article"), collection: @discussion)
@newPostView.on "thread:created", @navigateToThread
allThreads: ->
@nav.updateSidebar() allThreads: ->
setActiveThread: =>
if @thread
@nav.setActiveThread(@thread.get("id"))
showThread: (forum_name, thread_id) ->
@thread = @discussion.get(thread_id)
@setActiveThread()
if(@main)
@main.undelegateEvents()
@main = new DiscussionThreadView(el: $(".discussion-column"), model: @thread)
@main.render()
@main.on "thread:responses:rendered", =>
@nav.updateSidebar() @nav.updateSidebar()
navigateToThread: (thread_id) => setActiveThread: =>
thread = @discussion.get(thread_id) if @thread
@navigate("#{thread.get("commentable_id")}/threads/#{thread_id}", trigger: true) @nav.setActiveThread(@thread.get("id"))
navigateToAllThreads: => showThread: (forum_name, thread_id) ->
console.log "navigating" @thread = @discussion.get(thread_id)
@navigate("", trigger: true) @setActiveThread()
if(@main)
@main.undelegateEvents()
@main = new DiscussionThreadView(el: $(".discussion-column"), model: @thread)
@main.render()
@main.on "thread:responses:rendered", =>
@nav.updateSidebar()
navigateToThread: (thread_id) =>
thread = @discussion.get(thread_id)
@navigate("#{thread.get("commentable_id")}/threads/#{thread_id}", trigger: true)
navigateToAllThreads: =>
console.log "navigating"
@navigate("", trigger: true)
DiscussionApp = if Backbone?
start: (elem)-> DiscussionApp =
# TODO: Perhaps eliminate usage of global variables when possible start: (elem)->
element = $(elem) # TODO: Perhaps eliminate usage of global variables when possible
window.$$course_id = element.data("course-id") element = $(elem)
user_info = element.data("user-info") window.$$course_id = element.data("course-id")
threads = element.data("threads") user_info = element.data("user-info")
content_info = element.data("content-info") threads = element.data("threads")
window.user = new DiscussionUser(user_info) content_info = element.data("content-info")
Content.loadContentInfos(content_info) window.user = new DiscussionUser(user_info)
discussion = new Discussion(threads) Content.loadContentInfos(content_info)
new DiscussionRouter({discussion: discussion}) discussion = new Discussion(threads)
Backbone.history.start({pushState: true, root: "/courses/#{$$course_id}/discussion/forum/"}) new DiscussionRouter({discussion: discussion})
Backbone.history.start({pushState: true, root: "/courses/#{$$course_id}/discussion/forum/"})
$ -> $ ->
$("section.discussion").each (index, elem) -> $("section.discussion").each (index, elem) ->
DiscussionApp.start(elem) DiscussionApp.start(elem)
class @DiscussionUser extends Backbone.Model if Backbone?
following: (thread) -> class @DiscussionUser extends Backbone.Model
_.include(@get('subscribed_thread_ids'), thread.id) following: (thread) ->
_.include(@get('subscribed_thread_ids'), thread.id)
voted: (thread) -> voted: (thread) ->
_.include(@get('upvoted_ids'), thread.id) _.include(@get('upvoted_ids'), thread.id)
vote: (thread) -> vote: (thread) ->
@get('upvoted_ids').push(thread.id) @get('upvoted_ids').push(thread.id)
thread.vote() thread.vote()
unvote: (thread) -> unvote: (thread) ->
@set('upvoted_ids', _.without(@get('upvoted_ids'), thread.id)) @set('upvoted_ids', _.without(@get('upvoted_ids'), thread.id))
thread.unvote() thread.unvote()
class @DiscussionUserProfileView extends Backbone.View if Backbone?
toggleModeratorStatus: (event) -> class @DiscussionUserProfileView extends Backbone.View
confirmValue = confirm("Are you sure?") toggleModeratorStatus: (event) ->
if not confirmValue then return confirmValue = confirm("Are you sure?")
$elem = $(event.target) if not confirmValue then return
if $elem.hasClass("sidebar-promote-moderator-button") $elem = $(event.target)
isModerator = true if $elem.hasClass("sidebar-promote-moderator-button")
else if $elem.hasClass("sidebar-revoke-moderator-button") isModerator = true
isModerator = false else if $elem.hasClass("sidebar-revoke-moderator-button")
else isModerator = false
console.error "unrecognized moderator status" else
return console.error "unrecognized moderator status"
url = DiscussionUtil.urlFor('update_moderator_status', $$profiled_user_id) return
DiscussionUtil.safeAjax url = DiscussionUtil.urlFor('update_moderator_status', $$profiled_user_id)
$elem: $elem DiscussionUtil.safeAjax
url: url $elem: $elem
type: "POST" url: url
dataType: 'json' type: "POST"
data: dataType: 'json'
is_moderator: isModerator data:
error: (response, textStatus, e) -> is_moderator: isModerator
console.log e error: (response, textStatus, e) ->
success: (response, textStatus) => console.log e
parent = @$el.parent() success: (response, textStatus) =>
@$el.replaceWith(response.html) parent = @$el.parent()
view = new DiscussionUserProfileView el: parent.children(".user-profile") @$el.replaceWith(response.html)
view = new DiscussionUserProfileView el: parent.children(".user-profile")
events: events:
"click .sidebar-toggle-moderator-button": "toggleModeratorStatus" "click .sidebar-toggle-moderator-button": "toggleModeratorStatus"
class @DiscussionContentView extends Backbone.View if Backbone?
class @DiscussionContentView extends Backbone.View
attrRenderer:
endorsed: (endorsed) -> attrRenderer:
if endorsed endorsed: (endorsed) ->
@$(".action-endorse").addClass("is-endorsed") if endorsed
else @$(".action-endorse").addClass("is-endorsed")
@$(".action-endorse").removeClass("is-endorsed") else
@$(".action-endorse").removeClass("is-endorsed")
closed: (closed) ->
return if not @$(".action-openclose").length closed: (closed) ->
return if not @$(".post-status-closed").length return if not @$(".action-openclose").length
if closed return if not @$(".post-status-closed").length
@$(".post-status-closed").show() if closed
@$(".action-openclose").html(@$(".action-openclose").html().replace("Close", "Open")) @$(".post-status-closed").show()
@$(".discussion-reply-new").hide() @$(".action-openclose").html(@$(".action-openclose").html().replace("Close", "Open"))
else @$(".discussion-reply-new").hide()
@$(".post-status-closed").hide() else
@$(".action-openclose").html(@$(".action-openclose").html().replace("Open", "Close")) @$(".post-status-closed").hide()
@$(".discussion-reply-new").show() @$(".action-openclose").html(@$(".action-openclose").html().replace("Open", "Close"))
@$(".discussion-reply-new").show()
voted: (voted) ->
voted: (voted) ->
votes_point: (votes_point) ->
votes_point: (votes_point) ->
comments_count: (comments_count) ->
comments_count: (comments_count) ->
subscribed: (subscribed) ->
if subscribed subscribed: (subscribed) ->
@$(".dogear").addClass("is-followed") if subscribed
else @$(".dogear").addClass("is-followed")
@$(".dogear").removeClass("is-followed")
ability: (ability) ->
for action, selector of @abilityRenderer
if not ability[action]
selector.disable.apply(@)
else else
selector.enable.apply(@) @$(".dogear").removeClass("is-followed")
abilityRenderer: ability: (ability) ->
editable: for action, selector of @abilityRenderer
enable: -> @$(".action-edit").closest("li").show() if not ability[action]
disable: -> @$(".action-edit").closest("li").hide() selector.disable.apply(@)
can_delete: else
enable: -> @$(".action-delete").closest("li").show() selector.enable.apply(@)
disable: -> @$(".action-delete").closest("li").hide()
can_endorse: abilityRenderer:
enable: -> @$(".action-endorse").css("cursor", "auto") editable:
disable: -> @$(".action-endorse").css("cursor", "default") enable: -> @$(".action-edit").closest("li").show()
can_openclose: disable: -> @$(".action-edit").closest("li").hide()
enable: -> @$(".action-openclose").closest("li").show() can_delete:
disable: -> @$(".action-openclose").closest("li").hide() enable: -> @$(".action-delete").closest("li").show()
disable: -> @$(".action-delete").closest("li").hide()
renderPartialAttrs: -> can_endorse:
for attr, value of @model.changedAttributes() enable: -> @$(".action-endorse").css("cursor", "auto")
if @attrRenderer[attr] disable: -> @$(".action-endorse").css("cursor", "default")
@attrRenderer[attr].apply(@, [value]) can_openclose:
enable: -> @$(".action-openclose").closest("li").show()
renderAttrs: -> disable: -> @$(".action-openclose").closest("li").hide()
for attr, value of @model.attributes
if @attrRenderer[attr] renderPartialAttrs: ->
@attrRenderer[attr].apply(@, [value]) for attr, value of @model.changedAttributes()
if @attrRenderer[attr]
$: (selector) -> @attrRenderer[attr].apply(@, [value])
@$local.find(selector)
renderAttrs: ->
initLocal: -> for attr, value of @model.attributes
@$local = @$el.children(".local") if @attrRenderer[attr]
if not @$local.length @attrRenderer[attr].apply(@, [value])
@$local = @$el
@$delegateElement = @$local $: (selector) ->
@$local.find(selector)
makeWmdEditor: (cls_identifier) =>
if not @$el.find(".wmd-panel").length initLocal: ->
DiscussionUtil.makeWmdEditor @$el, $.proxy(@$, @), cls_identifier @$local = @$el.children(".local")
if not @$local.length
getWmdEditor: (cls_identifier) => @$local = @$el
DiscussionUtil.getWmdEditor @$el, $.proxy(@$, @), cls_identifier @$delegateElement = @$local
getWmdContent: (cls_identifier) => makeWmdEditor: (cls_identifier) =>
DiscussionUtil.getWmdContent @$el, $.proxy(@$, @), cls_identifier if not @$el.find(".wmd-panel").length
DiscussionUtil.makeWmdEditor @$el, $.proxy(@$, @), cls_identifier
setWmdContent: (cls_identifier, text) =>
DiscussionUtil.setWmdContent @$el, $.proxy(@$, @), cls_identifier, text getWmdEditor: (cls_identifier) =>
DiscussionUtil.getWmdEditor @$el, $.proxy(@$, @), cls_identifier
initialize: ->
@initLocal() getWmdContent: (cls_identifier) =>
@model.bind('change', @renderPartialAttrs, @) DiscussionUtil.getWmdContent @$el, $.proxy(@$, @), cls_identifier
setWmdContent: (cls_identifier, text) =>
DiscussionUtil.setWmdContent @$el, $.proxy(@$, @), cls_identifier, text
initialize: ->
@initLocal()
@model.bind('change', @renderPartialAttrs, @)
class @DiscussionThreadView extends DiscussionContentView if Backbone?
class @DiscussionThreadView extends DiscussionContentView
events: events:
"click .discussion-submit-post": "submitComment" "click .discussion-submit-post": "submitComment"
template: _.template($("#thread-template").html()) template: _.template($("#thread-template").html())
$: (selector) -> $: (selector) ->
@$el.find(selector) @$el.find(selector)
initialize: -> initialize: ->
super() super()
@showView = new DiscussionThreadShowView(model: @model) @showView = new DiscussionThreadShowView(model: @model)
@showView.bind "thread:delete", @delete @showView.bind "thread:delete", @delete
@showView.bind "thread:edit", @edit @showView.bind "thread:edit", @edit
render: -> render: ->
@$el.html(@template(@model.toJSON())) @$el.html(@template(@model.toJSON()))
@delegateEvents() @delegateEvents()
@showView.setElement(@$('.thread-content-wrapper')) @showView.setElement(@$('.thread-content-wrapper'))
@showView.render() @showView.render()
@showView.delegateEvents() @showView.delegateEvents()
@renderAttrs() @renderAttrs()
@$("span.timeago").timeago() @$("span.timeago").timeago()
@makeWmdEditor "reply-body" @makeWmdEditor "reply-body"
@renderResponses() @renderResponses()
@ @
renderResponses: -> renderResponses: ->
DiscussionUtil.safeAjax DiscussionUtil.safeAjax
url: "/courses/#{$$course_id}/discussion/forum/#{@model.get('commentable_id')}/threads/#{@model.id}" url: "/courses/#{$$course_id}/discussion/forum/#{@model.get('commentable_id')}/threads/#{@model.id}"
success: (data, textStatus, xhr) => success: (data, textStatus, xhr) =>
@$el.find(".loading").remove() @$el.find(".loading").remove()
Content.loadContentInfos(data['annotated_content_info']) Content.loadContentInfos(data['annotated_content_info'])
comments = new Comments(data['content']['children']) comments = new Comments(data['content']['children'])
comments.each @renderResponse comments.each @renderResponse
@trigger "thread:responses:rendered" @trigger "thread:responses:rendered"
renderResponse: (response) => renderResponse: (response) =>
response.set('thread', @model) response.set('thread', @model)
view = new ThreadResponseView(model: response) view = new ThreadResponseView(model: response)
view.on "comment:add", @addComment view.on "comment:add", @addComment
view.on "comment:endorse", @endorseThread view.on "comment:endorse", @endorseThread
view.render() view.render()
@$el.find(".responses").append(view.el) @$el.find(".responses").append(view.el)
addComment: => addComment: =>
@model.comment() @model.comment()
endorseThread: (endorsed) => endorseThread: (endorsed) =>
is_endorsed = @$el.find(".is-endorsed").length is_endorsed = @$el.find(".is-endorsed").length
@model.set 'endorsed', is_endorsed @model.set 'endorsed', is_endorsed
submitComment: (event) -> submitComment: (event) ->
event.preventDefault() event.preventDefault()
url = @model.urlFor('reply') url = @model.urlFor('reply')
body = @getWmdContent("reply-body") body = @getWmdContent("reply-body")
return if not body.trim().length return if not body.trim().length
@setWmdContent("reply-body", "") @setWmdContent("reply-body", "")
comment = new Comment(body: body, created_at: (new Date()).toISOString(), username: window.user.get("username"), votes: { up_count: 0 }, endorsed: false, user_id: window.user.get("id")) comment = new Comment(body: body, created_at: (new Date()).toISOString(), username: window.user.get("username"), votes: { up_count: 0 }, endorsed: false, user_id: window.user.get("id"))
comment.set('thread', @model.get('thread')) comment.set('thread', @model.get('thread'))
@renderResponse(comment) @renderResponse(comment)
@model.addComment() @model.addComment()
DiscussionUtil.safeAjax DiscussionUtil.safeAjax
$elem: $(event.target) $elem: $(event.target)
url: url url: url
type: "POST" type: "POST"
dataType: 'json' dataType: 'json'
data: data:
body: body body: body
success: (data, textStatus) => success: (data, textStatus) =>
comment.updateInfo(data.annotated_content_info) comment.updateInfo(data.annotated_content_info)
comment.set(data.content) comment.set(data.content)
edit: -> edit: ->
delete: (event) -> delete: (event) ->
url = @model.urlFor('delete') url = @model.urlFor('delete')
if not @model.can('can_delete') if not @model.can('can_delete')
return return
if not confirm "Are you sure to delete thread \"#{@model.get('title')}\"?" if not confirm "Are you sure to delete thread \"#{@model.get('title')}\"?"
return return
@model.remove() @model.remove()
@$el.empty() @$el.empty()
$elem = $(event.target) $elem = $(event.target)
DiscussionUtil.safeAjax DiscussionUtil.safeAjax
$elem: $elem $elem: $elem
url: url url: url
type: "POST" type: "POST"
success: (response, textStatus) => success: (response, textStatus) =>
class @NewPostInlineView extends Backbone.View if Backbone?
class @NewPostInlineView extends Backbone.View
initialize: () ->
initialize: () ->
@topicId = @$(".topic").first().data("discussion-id")
@topicId = @$(".topic").first().data("discussion-id")
@maxNameWidth = 100
@maxNameWidth = 100
DiscussionUtil.makeWmdEditor @$el, $.proxy(@$, @), "new-post-body"
@$(".new-post-tags").tagsInput DiscussionUtil.tagsInputOptions() DiscussionUtil.makeWmdEditor @$el, $.proxy(@$, @), "new-post-body"
@$(".new-post-tags").tagsInput DiscussionUtil.tagsInputOptions()
events:
"submit .new-post-form": "createPost" events:
"submit .new-post-form": "createPost"
# Because we want the behavior that when the body is clicked the menu is
# closed, we need to ignore clicks in the search field and stop propagation. # Because we want the behavior that when the body is clicked the menu is
# Without this, clicking the search field would also close the menu. # closed, we need to ignore clicks in the search field and stop propagation.
ignoreClick: (event) -> # Without this, clicking the search field would also close the menu.
event.stopPropagation() ignoreClick: (event) ->
event.stopPropagation()
createPost: (event) ->
event.preventDefault() createPost: (event) ->
title = @$(".new-post-title").val() event.preventDefault()
body = @$(".new-post-body").find(".wmd-input").val() title = @$(".new-post-title").val()
tags = @$(".new-post-tags").val() body = @$(".new-post-body").find(".wmd-input").val()
tags = @$(".new-post-tags").val()
anonymous = false || @$("input.discussion-anonymous").is(":checked")
follow = false || @$("input.discussion-follow").is(":checked") anonymous = false || @$("input.discussion-anonymous").is(":checked")
follow = false || @$("input.discussion-follow").is(":checked")
url = DiscussionUtil.urlFor('create_thread', @topicId)
url = DiscussionUtil.urlFor('create_thread', @topicId)
DiscussionUtil.safeAjax
$elem: $(event.target) DiscussionUtil.safeAjax
$loading: $(event.target) if event $elem: $(event.target)
url: url $loading: $(event.target) if event
type: "POST" url: url
dataType: 'json' type: "POST"
async: false # TODO when the rest of the stuff below is made to work properly.. dataType: 'json'
data: async: false # TODO when the rest of the stuff below is made to work properly..
title: title data:
body: body title: title
tags: tags body: body
anonymous: anonymous tags: tags
auto_subscribe: follow anonymous: anonymous
error: DiscussionUtil.formErrorHandler(@$(".new-post-form-errors")) auto_subscribe: follow
success: (response, textStatus) => error: DiscussionUtil.formErrorHandler(@$(".new-post-form-errors"))
# TODO: Move this out of the callback, this makes it feel sluggish success: (response, textStatus) =>
thread = new Thread response['content'] # TODO: Move this out of the callback, this makes it feel sluggish
DiscussionUtil.clearFormErrors(@$(".new-post-form-errors")) thread = new Thread response['content']
@$el.hide() DiscussionUtil.clearFormErrors(@$(".new-post-form-errors"))
@$(".new-post-title").val("").attr("prev-text", "") @$el.hide()
@$(".new-post-body textarea").val("").attr("prev-text", "") @$(".new-post-title").val("").attr("prev-text", "")
@$(".new-post-tags").val("") @$(".new-post-body textarea").val("").attr("prev-text", "")
@$(".new-post-tags").importTags("") @$(".new-post-tags").val("")
@collection.add thread @$(".new-post-tags").importTags("")
@collection.add thread
class @ResponseCommentView extends DiscussionContentView if Backbone?
tagName: "li" class @ResponseCommentView extends DiscussionContentView
template: _.template($("#response-comment-template").html()) tagName: "li"
initLocal: -> template: _.template($("#response-comment-template").html())
# TODO .response-local is the parent of the comments so @$local is null, not sure what was intended here... initLocal: ->
@$local = @$el.find(".response-local") # TODO .response-local is the parent of the comments so @$local is null, not sure what was intended here...
@$delegateElement = @$local @$local = @$el.find(".response-local")
@$delegateElement = @$local
render: -> render: ->
@$el.html(@template(@model.toJSON())) @$el.html(@template(@model.toJSON()))
@initLocal() @initLocal()
@delegateEvents() @delegateEvents()
@renderAttrs() @renderAttrs()
@$el.find(".timeago").timeago() @$el.find(".timeago").timeago()
@convertMath() @convertMath()
@ @
convertMath: -> convertMath: ->
body = @$el.find(".response-body") body = @$el.find(".response-body")
body.html DiscussionUtil.postMathJaxProcessor DiscussionUtil.markdownWithHighlight body.html() body.html DiscussionUtil.postMathJaxProcessor DiscussionUtil.markdownWithHighlight body.html()
# This removes paragraphs so that comments are more compact # This removes paragraphs so that comments are more compact
body.children("p").each (index, elem) -> body.children("p").each (index, elem) ->
$(elem).replaceWith($(elem).html()) $(elem).replaceWith($(elem).html())
MathJax.Hub.Queue ["Typeset", MathJax.Hub, body[0]] MathJax.Hub.Queue ["Typeset", MathJax.Hub, body[0]]
class @ThreadResponseView extends DiscussionContentView if Backbone?
tagName: "li" class @ThreadResponseView extends DiscussionContentView
template: _.template($("#thread-response-template").html()) tagName: "li"
template: _.template($("#thread-response-template").html())
events: events:
"click .vote-btn": "toggleVote" "click .vote-btn": "toggleVote"
"submit .comment-form": "submitComment" "submit .comment-form": "submitComment"
"click .action-endorse": "toggleEndorse" "click .action-endorse": "toggleEndorse"
"click .action-delete": "delete" "click .action-delete": "delete"
render: -> render: ->
@$el.html(@template(@model.toJSON())) @$el.html(@template(@model.toJSON()))
@initLocal() @initLocal()
@delegateEvents() @delegateEvents()
if window.user.voted(@model) if window.user.voted(@model)
@$(".vote-btn").addClass("is-cast") @$(".vote-btn").addClass("is-cast")
@renderAttrs() @renderAttrs()
@$el.find(".posted-details").timeago() @$el.find(".posted-details").timeago()
@convertMath() @convertMath()
@renderComments() @renderComments()
@ @
convertMath: -> convertMath: ->
element = @$(".response-body") element = @$(".response-body")
element.html DiscussionUtil.postMathJaxProcessor DiscussionUtil.markdownWithHighlight element.html() element.html DiscussionUtil.postMathJaxProcessor DiscussionUtil.markdownWithHighlight element.html()
MathJax.Hub.Queue ["Typeset", MathJax.Hub, element[0]] MathJax.Hub.Queue ["Typeset", MathJax.Hub, element[0]]
renderComments: -> renderComments: ->
@model.get("comments").each @renderComment @model.get("comments").each @renderComment
renderComment: (comment) => renderComment: (comment) =>
comment.set('thread', @model.get('thread')) comment.set('thread', @model.get('thread'))
view = new ResponseCommentView(model: comment) view = new ResponseCommentView(model: comment)
view.render() view.render()
@$el.find(".comments li:last").before(view.el) @$el.find(".comments li:last").before(view.el)
toggleVote: (event) -> toggleVote: (event) ->
event.preventDefault() event.preventDefault()
@$(".vote-btn").toggleClass("is-cast") @$(".vote-btn").toggleClass("is-cast")
if @$(".vote-btn").hasClass("is-cast") if @$(".vote-btn").hasClass("is-cast")
@vote() @vote()
else else
@unvote() @unvote()
vote: -> vote: ->
url = @model.urlFor("upvote") url = @model.urlFor("upvote")
@$(".votes-count-number").html(parseInt(@$(".votes-count-number").html()) + 1) @$(".votes-count-number").html(parseInt(@$(".votes-count-number").html()) + 1)
DiscussionUtil.safeAjax DiscussionUtil.safeAjax
$elem: @$(".discussion-vote") $elem: @$(".discussion-vote")
url: url url: url
type: "POST" type: "POST"
success: (response, textStatus) => success: (response, textStatus) =>
if textStatus == 'success' if textStatus == 'success'
@model.set(response) @model.set(response)
unvote: -> unvote: ->
url = @model.urlFor("unvote") url = @model.urlFor("unvote")
@$(".votes-count-number").html(parseInt(@$(".votes-count-number").html()) - 1) @$(".votes-count-number").html(parseInt(@$(".votes-count-number").html()) - 1)
DiscussionUtil.safeAjax DiscussionUtil.safeAjax
$elem: @$(".discussion-vote") $elem: @$(".discussion-vote")
url: url url: url
type: "POST" type: "POST"
success: (response, textStatus) => success: (response, textStatus) =>
if textStatus == 'success' if textStatus == 'success'
@model.set(response) @model.set(response)
submitComment: (event) -> submitComment: (event) ->
event.preventDefault() event.preventDefault()
url = @model.urlFor('reply') url = @model.urlFor('reply')
body = @$(".comment-form-input").val() body = @$(".comment-form-input").val()
if not body.trim().length if not body.trim().length
return return
comment = new Comment(body: body, created_at: (new Date()).toISOString(), username: window.user.get("username"), user_id: window.user.get("id")) comment = new Comment(body: body, created_at: (new Date()).toISOString(), username: window.user.get("username"), user_id: window.user.get("id"))
@renderComment(comment) @renderComment(comment)
@trigger "comment:add", comment @trigger "comment:add", comment
@$(".comment-form-input").val("") @$(".comment-form-input").val("")
DiscussionUtil.safeAjax DiscussionUtil.safeAjax
$elem: $(event.target) $elem: $(event.target)
url: url url: url
type: "POST" type: "POST"
dataType: 'json' dataType: 'json'
data: data:
body: body body: body
delete: (event) -> delete: (event) ->
event.preventDefault() event.preventDefault()
if not @model.can('can_delete') if not @model.can('can_delete')
return return
console.log $(event.target) console.log $(event.target)
url = @model.urlFor('delete') url = @model.urlFor('delete')
if not confirm "Are you sure to delete this response? " if not confirm "Are you sure to delete this response? "
return return
@model.remove() @model.remove()
@$el.remove() @$el.remove()
$elem = $(event.target) $elem = $(event.target)
DiscussionUtil.safeAjax DiscussionUtil.safeAjax
$elem: $elem $elem: $elem
url: url url: url
type: "POST" type: "POST"
success: (response, textStatus) => success: (response, textStatus) =>
toggleEndorse: (event) -> toggleEndorse: (event) ->
event.preventDefault() event.preventDefault()
if not @model.can('can_endorse') if not @model.can('can_endorse')
return return
$elem = $(event.target) $elem = $(event.target)
url = @model.urlFor('endorse') url = @model.urlFor('endorse')
endorsed = @model.get('endorsed') endorsed = @model.get('endorsed')
data = { endorsed: not endorsed } data = { endorsed: not endorsed }
@model.set('endorsed', not endorsed) @model.set('endorsed', not endorsed)
@trigger "comment:endorse", not endorsed @trigger "comment:endorse", not endorsed
DiscussionUtil.safeAjax DiscussionUtil.safeAjax
$elem: $elem $elem: $elem
url: url url: url
data: data data: data
type: "POST" type: "POST"
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