PageRenderTime 51ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 1ms

/app/client/answer.coffee

https://bitbucket.org/pierroweb/learn-addict
CoffeeScript | 71 lines | 57 code | 13 blank | 1 comment | 4 complexity | 3dffeb95230cf1a40af348bc2d34621b MD5 | raw file
  1. # Answer, AnswerView, AnswerList
  2. exports.Answer = Backbone.Model.extend(defaults: ->
  3. id: "0"
  4. user: "Marius ID"
  5. name: "donkey"
  6. image: "http://graph.facebook.com/219015/picture"
  7. round_id: ""
  8. content: "Problem loading the answer."
  9. date: new Date
  10. comments: new SS.client.comment.CommentList()
  11. score: 0
  12. mine: false
  13. )
  14. exports.AnswerView = Backbone.View.extend(
  15. tagName: "div",
  16. className: "answer",
  17. template: _.template($("#answer-template").html())
  18. events:
  19. "click .sendComment": "sendComment"
  20. "click .rateAnswer" : "rateAnswer"
  21. initialize: ->
  22. _.bindAll this, "render", "appendComment", "sendComment", "rateAnswer", "clear", "updateScore"
  23. if @model
  24. @model.get("comments").bind "add", @appendComment
  25. @model.bind "clear", @clear
  26. @model.bind "change:score", @updateScore
  27. render: ->
  28. if @model
  29. $(@el).html @template(@model.toJSON())
  30. _.each @model.get("comments").models, @appendComment
  31. this
  32. appendComment: (comment) ->
  33. commentView = new SS.client.comment.CommentView(model: comment)
  34. window.appView.commentViews.push commentView
  35. $("ul.comments", @el).append commentView.render().el
  36. sendComment: ->
  37. comment =
  38. round_id: @model.get("round_id")
  39. answer_id: @model.get("id")
  40. to_user: @model.get("user")
  41. content: $('input.comment', @el).val()
  42. SS.server.app.sendComment comment, myChannel, (response) -> SS.client.app.displayFlash response
  43. $('input.comment', @el).val('')
  44. rateAnswer: ->
  45. n = $('#nbrVotesAnswer span').text()
  46. if 1*n > 0 then $('#nbrVotesAnswer span').text(-1+1*n)
  47. rate =
  48. answer_id: @model.get("id")
  49. round_id: @model.get("round_id")
  50. rated_user: @model.get("user")
  51. SS.server.app.rateAnswer rate, myChannel, (response) -> SS.client.app.displayFlash response
  52. clear: ->
  53. if @model then _.each @model.get("comments").models, (comment) -> comment.trigger "clear"
  54. @remove()
  55. updateScore: ->
  56. this.$(".scoreAnswer").html @model.get("score")
  57. )
  58. exports.AnswerList = Backbone.Collection.extend(model: SS.client.answer.Answer)