/app/client/answer.coffee
CoffeeScript | 71 lines | 57 code | 13 blank | 1 comment | 4 complexity | 3dffeb95230cf1a40af348bc2d34621b MD5 | raw file
- # Answer, AnswerView, AnswerList
- exports.Answer = Backbone.Model.extend(defaults: ->
- id: "0"
- user: "Marius ID"
- name: "donkey"
- image: "http://graph.facebook.com/219015/picture"
- round_id: ""
- content: "Problem loading the answer."
- date: new Date
- comments: new SS.client.comment.CommentList()
- score: 0
- mine: false
- )
- exports.AnswerView = Backbone.View.extend(
- tagName: "div",
- className: "answer",
- template: _.template($("#answer-template").html())
- events:
- "click .sendComment": "sendComment"
- "click .rateAnswer" : "rateAnswer"
- initialize: ->
- _.bindAll this, "render", "appendComment", "sendComment", "rateAnswer", "clear", "updateScore"
- if @model
- @model.get("comments").bind "add", @appendComment
- @model.bind "clear", @clear
- @model.bind "change:score", @updateScore
- render: ->
- if @model
- $(@el).html @template(@model.toJSON())
- _.each @model.get("comments").models, @appendComment
- this
- appendComment: (comment) ->
- commentView = new SS.client.comment.CommentView(model: comment)
- window.appView.commentViews.push commentView
- $("ul.comments", @el).append commentView.render().el
-
- sendComment: ->
- comment =
- round_id: @model.get("round_id")
- answer_id: @model.get("id")
- to_user: @model.get("user")
- content: $('input.comment', @el).val()
- SS.server.app.sendComment comment, myChannel, (response) -> SS.client.app.displayFlash response
- $('input.comment', @el).val('')
-
- rateAnswer: ->
- n = $('#nbrVotesAnswer span').text()
- if 1*n > 0 then $('#nbrVotesAnswer span').text(-1+1*n)
- rate =
- answer_id: @model.get("id")
- round_id: @model.get("round_id")
- rated_user: @model.get("user")
- SS.server.app.rateAnswer rate, myChannel, (response) -> SS.client.app.displayFlash response
-
-
- clear: ->
- if @model then _.each @model.get("comments").models, (comment) -> comment.trigger "clear"
- @remove()
-
- updateScore: ->
- this.$(".scoreAnswer").html @model.get("score")
- )
- exports.AnswerList = Backbone.Collection.extend(model: SS.client.answer.Answer)