Created
October 13, 2011 20:06
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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