Skip to content

Instantly share code, notes, and snippets.

@dmexe
Created October 13, 2011 20:06
Show Gist options
  • Save dmexe/1285351 to your computer and use it in GitHub Desktop.
Save dmexe/1285351 to your computer and use it in GitHub Desktop.
class Fashionclub.Views.CommentsView extends Backbone.View
events:
"submit .comment-form" : "onSubmit"
"click .comment .edit-comment" : "onEdit"
"click .comment .destroy-comment": "onDestroy"
"click .comment-form .cancel-comment" : "onCancelEdit"
initialize: ->
_.bindAll(this)
@collection = new Fashionclub.Collections.CommentsCollection
@collection.bind "reset", @render
@collection.fetchWith @params()
render: ->
@el.html @jst("comments/index")
onSubmit: (ev) ->
ev.preventDefault()
form = $(ev.target)
if comment = @collection.getBy(form)
comment.saveWith form, url: @params(), success: @update
else
@collection.createWith form, url: @params(), success: @create
onEdit: (ev) ->
ev.preventDefault()
comment = @collection.getBy(ev.target)
@domFor(comment).after(@jst("comments/_form", comment:comment)).hide()
onDestroy: (ev) ->
ev.preventDefault()
comment = @collection.getBy(ev.target)
comment.destroy url:@params()
@domFor(comment).slideUp (-> $(this).remove() )
onCancelEdit: (ev) ->
ev.preventDefault()
form = $(ev.target).closest("form")
dom = form.prev(".comment").show()
form.remove()
create: (comment) ->
$(".comment:last", @el).after @jst("comments/_comment", comment:comment)
@domFor(comment).hide().slideDown()
@domFor(comment, "form", "new").get(0).reset()
update: (comment) ->
@domFor(comment).replaceWith @jst("comments/_comment", comment:comment)
@domFor(comment, "form").remove()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment