Commit 12eeb30c by Rocky Duan

delete threads

parent d9edc1b4
......@@ -36,12 +36,14 @@ if Backbone?
@get('children').push comment
model = new Comment $.extend {}, comment, { thread: @get('thread') }
@get('comments').add model
@trigger "comment:add"
model
removeComment: (comment) ->
thread = @get('thread')
comments_count = parseInt(thread.get('comments_count'))
thread.set('comments_count', comments_count - 1 - comment.getCommentsCount())
@trigger "comment:remove"
resetComments: (children) ->
@set 'children', []
......@@ -56,6 +58,14 @@ if Backbone?
@set 'user_url', DiscussionUtil.urlFor('user_profile', @get('user_id'))
@resetComments(@get('children'))
remove: ->
if @get('type') == 'comment'
@get('thread').removeComment(@)
@get('thread').trigger "comment:remove", @
else
@trigger "thread:remove", @
@addContent: (id, content) -> @contents[id] = content
@getContent: (id) -> @contents[id]
......
......@@ -6,6 +6,9 @@ if Backbone?
@bind "add", (item) =>
item.discussion = @
@comparator = @sortByDateRecentFirst
@on "thread:remove", (thread) =>
console.log "remove triggered"
@remove(thread)
find: (id) ->
_.first @where(id: id)
......
......@@ -7,6 +7,7 @@ class @DiscussionRouter extends Backbone.Router
@discussion = options['discussion']
@nav = new DiscussionThreadListView(collection: @discussion, el: $(".sidebar"))
@nav.on "thread:selected", @navigateToThread
@nav.on "thread:removed", @navigateToAllThreads
@nav.on "threads:rendered", @setActiveThread
@nav.render()
......@@ -33,3 +34,7 @@ class @DiscussionRouter extends Backbone.Router
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)
......@@ -21,6 +21,7 @@ class @DiscussionThreadListView extends Backbone.View
@timer = 0
@$el.html(@template())
@displayedCollection.on "reset", @renderThreads
@displayedCollection.on "thread:remove", @renderThreads
@renderThreads()
@
......@@ -30,8 +31,10 @@ class @DiscussionThreadListView extends Backbone.View
@trigger "threads:rendered"
renderThreadListItem: (thread) =>
console.log "rendering"
view = new ThreadListItemView(model: thread)
view.on "thread:selected", @threadSelected
view.on "thread:removed", @threadRemoved
view.render()
@$(".post-list").append(view.el)
......@@ -39,6 +42,9 @@ class @DiscussionThreadListView extends Backbone.View
@setActiveThread(thread_id)
@trigger("thread:selected", thread_id)
threadRemoved: (thread_id) =>
@trigger("thread:removed", thread_id)
setActiveThread: (thread_id) ->
@$(".post-list a").removeClass("active")
@$("a[data-id='#{thread_id}']").addClass("active")
......
......@@ -60,7 +60,7 @@ class @DiscussionThreadView extends DiscussionContentView
@$(".responses").append(view.el)
addComment: =>
@model.trigger "comment:add"
toggleVote: (event) ->
event.preventDefault()
......@@ -110,8 +110,9 @@ class @DiscussionThreadView extends DiscussionContentView
url = @model.urlFor('reply')
body = @$("#wmd-input").val()
response = 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"))
response.set('thread', @model.get('thread'))
@renderResponse(response)
@addComment()
@model.addComment()
DiscussionUtil.safeAjax
$elem: $(event.target)
......@@ -123,8 +124,18 @@ class @DiscussionThreadView extends DiscussionContentView
edit: ->
delete: ->
delete: (event) ->
url = @model.urlFor('delete')
if not confirm "Are you sure to delete thread \"#{@model.get('title')}\"?"
return
@model.remove()
$elem = $(event.target)
DiscussionUtil.safeAjax
$elem: $elem
url: url
type: "POST"
success: (response, textStatus) =>
toggleClosed: (event) ->
$elem = $(event.target)
url = @model.urlFor('close')
......
......@@ -5,23 +5,32 @@ class @ThreadListItemView extends Backbone.View
"click a": "threadSelected"
initialize: ->
@model.on "change", @render
@model.on "thread:remove", @threadRemoved
@model.on "thread:follow", @follow
@model.on "thread:unfollow", @unfollow
@model.on "comment:add", @addComment
@model.on "comment:remove", @removeComment
render: =>
@$el.html(@template(@model.toJSON()))
if window.user.following(@model)
@follow()
@
threadSelected: (event) ->
event.preventDefault()
@trigger("thread:selected", @model.id)
threadRemoved: =>
@trigger("thread:removed", @model.id)
follow: =>
@$("a").addClass("followed")
unfollow: =>
@$("a").removeClass("followed")
addComment: =>
@$(".comments-count").html(parseInt(@$(".comments-count").html()) + 1)
addComment: (comment) =>
@$(".comments-count").html(@model.get('comments_count'))
removeComment: (comment) =>
@$(".comments-count").html(@model.get('comments_count'))
......@@ -68,7 +68,7 @@ class @ThreadResponseView extends DiscussionContentView
return
comment = new Comment(body: body, created_at: (new Date()).toISOString(), username: window.user.get("username"), user_id: window.user.get("id"))
@renderComment(comment)
@trigger "comment:add"
@trigger "comment:add", comment
@$(".comment-form-input").val("")
DiscussionUtil.safeAjax
......
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