Skip to content

Instantly share code, notes, and snippets.

@dmexe
Created October 13, 2011 20:06
Show Gist options
  • Save dmexe/1285348 to your computer and use it in GitHub Desktop.
Save dmexe/1285348 to your computer and use it in GitHub Desktop.
class App.Model.Comment
@list: (owner) ->
App.Model.request('GET', @url(owner))
@create: (owner, comment) ->
App.Model.request('POST', @url(owner), comment)
@update: (owner, id, comment) ->
App.Model.request('PUT', @url(owner, id), comment)
@destroy: (owner, id) ->
App.Model.request('DELETE', @url(owner, id))
@url: (owner, id = null) ->
prefix = ""
if owner["video_id"]
prefix = "/videos/#{owner["video_id"]}"
else if owner["user_id"]
prefix = "/users/#{owner["user_id"]}"
else if owner["photo_id"]
prefix = "/photos/#{owner["photo_id"]}"
else if owner["blog_post_id"]
prefix = "/blog_posts/#{owner["blog_post_id"]}"
prefix += "/comments"
prefix += "/#{id}" if id
"#{prefix}.json"
class App.Comments
@init: ->
$(".comments-container").each ->
#new App.Comments($(this))
constructor:(root) ->
this.model = App.Model.Comment
this.cont = root
this.findOwner()
this.loadList()
this.observe()
findOwner: ->
this.owner = {}
if v = this.cont.data("video_id")
this.owner["video_id"] = v
else if v = this.cont.data("user_id")
this.owner["user_id"] = v
else if v = this.cont.data("photo_id")
this.owner["photo_id"] = v
else if v = this.cont.data("blog_post_id")
this.owner["blog_post_id"] = v
loadList: ->
self = this
this.model.list(this.owner).done (data) ->
self.renderList(data)
renderList:(data) ->
this.cont.html("")
$("#comments-list-tmpl").tmpl(comments:data,owner_user_id:this.cont.data("user_id")).appendTo(this.cont)
$("#comments-edit-tmpl").tmpl(type:"POST", id:"new", content: "", image:App.currentUser.image).appendTo(this.cont)
$(".comment", this.cont).show()
App.resizeIframe()
observe: ->
self = this
$("form.comment-form", this.cont).live "submit", (ev) ->
self.submitForm($(this))
return false
$("a.save-comment", this.cont).live "click", (ev) ->
$(ev.target).closest("form").submit()
$("a.edit-comment", this.cont).live "click", (ev) ->
self.editComment($(this).closest(".comment"))
$("a.cancel-comment", this.cont).live "click", (ev) ->
self.cancelEdit($(this).closest("form"))
$("a.destroy-comment", this.cont).live "click", (ev) ->
if confirm("Вы увенены?")
self.destroyComment($(this).closest(".comment"))
submitForm:(form) ->
self = this
data = form.serialize()
if form.data("id") == "new"
this.model.create(this.owner, data).done (data) ->
self.clearForm(form)
self.appendComment(data)
else
this.model.update(this.owner, form.data("id"), data).done (data) ->
form.remove()
self.replaceComment(data)
clearForm:(form) ->
form.get(0).reset()
editComment:(com) ->
id = com.data("id")
cont = com.find(".comment-content").text()
img = com.find("img").attr("src")
$("#comments-edit-tmpl").tmpl(type:"PUT", id:id, content:cont, image:img).insertAfter(com)
com.hide()
cancelEdit:(form) ->
id = form.data("id")
com = $(".comment[data-id=#{id}]")
form.remove()
com.show()
destroyComment:(comment) ->
id = comment.data("id")
this.model.destroy(this.owner, id).done ->
App.resizeIframe()
comment.slideUp ->
comment.remove()
appendComment:(comment) ->
last = $(".comment:last", this.cont)
tmpl = $("#comments-list-tmpl").tmpl({comments:[comment], hidden:"1"})
if last.size() != 0
tmpl.insertAfter(last)
else
this.cont.prepend(tmpl)
$(".comment:hidden", this.cont).slideDown ->
App.resizeIframe()
replaceComment:(comment) ->
com = $(".comment[data-id=#{comment.id}]")
$("#comments-list-tmpl").tmpl({comments:[comment]}).insertAfter(com)
com.remove()
$(document).ready ->
App.Comments.init()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment