/web/app/assets/javascripts/coffeescripts/comment.js

https://github.com/hiphapis/tide_web · JavaScript · 231 lines · 166 code · 59 blank · 6 comment · 20 complexity · 496e330c1315967bddd8470047354bbe MD5 · raw file

  1. $(function(){
  2. if (window._CommentApp) {
  3. // model: Comment
  4. window.Comment = Backbone.Model.extend({
  5. defaults: {
  6. body: "",
  7. user_id: "",
  8. post_id: "",
  9. positive_agreements_count: 0,
  10. negative_agreements_count: 0
  11. },
  12. initialize: function() {
  13. }
  14. });
  15. window.CommentList = Backbone.Collection.extend({
  16. model: Comment,
  17. url :'/posts/' + window._postID + '/comments'
  18. });
  19. window.Comments = new window.CommentList;
  20. // model: Agreement
  21. window.Agreement = Backbone.Model.extend({
  22. defaults: {
  23. post_id: "",
  24. event_id: "",
  25. comment_id: "",
  26. user_id: "",
  27. direction: 0
  28. },
  29. initialize: function() {
  30. }
  31. });
  32. window.AgreementList = Backbone.Collection.extend({
  33. model: Agreement,
  34. url :'/agreements/'
  35. });
  36. window.Agreements = new window.AgreementList;
  37. // view: Comment
  38. window.CommentView = Backbone.View.extend({
  39. tagName: "li",
  40. className: "comment",
  41. template: $('#comment_template'),
  42. edit_template: $('#comment_edit_template'),
  43. events: {
  44. "click .edit_button" : "edit",
  45. "click .cancel_button" : "cancel",
  46. "click .destroy_button" : "destroy",
  47. "submit form.edit_comment" : "save",
  48. "click a.agree_positive" : "agree_positive",
  49. "click a.agree_negative" : "agree_negative"
  50. },
  51. initialize: function() {
  52. this.model.bind('change', this.render, this);
  53. this.model.bind('destroy', this.remove, this);
  54. },
  55. render: function() {
  56. this.show();
  57. return this;
  58. },
  59. edit: function() {
  60. $(this.el).html(this.edit_template.tmpl(this.model.toJSON()));
  61. return false;
  62. },
  63. cancel: function() {
  64. this.show();
  65. return false;
  66. },
  67. show: function() {
  68. $(this.el).html(this.template.tmpl(this.model.toJSON()));
  69. },
  70. save: function() {
  71. var input = $("form.edit_comment textarea");
  72. var text = input.val();
  73. if (!text || text.length < 1) {
  74. alert("내용을 입력해주세요.");
  75. return false;
  76. }
  77. this.model.save({ body: text });
  78. this.show();
  79. return false;
  80. },
  81. destroy: function() {
  82. if (confirm("Are you sure?")) {
  83. this.model.destroy();
  84. $(this.el).remove();
  85. }
  86. return false;
  87. },
  88. should_create_agreement_model: true,
  89. agree_positive: function() {
  90. if (!window._userID) {
  91. alert("로그인이 필요합니다.");
  92. return false;
  93. }
  94. var a = this.current_comment_agreement();
  95. if (a) {
  96. if (a.get("direction") < 0) {
  97. a.save({ direction:1 });
  98. this.model.set({
  99. positive_agreements_count:(this.model.get("positive_agreements_count") + 1),
  100. negative_agreements_count:(this.model.get("negative_agreements_count") - 1)
  101. });
  102. }
  103. } else if (this.should_create_agreement_model) {
  104. this.should_create_agreement_model = false;
  105. var ag = Agreements.create({ post_id:window._postID, event_id:window._eventID, comment_id:this.model.id, user_id:window._userID, direction:1 });
  106. this.model.set({ positive_agreements_count:(this.model.get("positive_agreements_count") + 1) });
  107. }
  108. return false;
  109. },
  110. agree_negative: function() {
  111. if (!window._userID) {
  112. alert("로그인이 필요합니다.");
  113. return false;
  114. }
  115. var a = this.current_comment_agreement();
  116. if (a) {
  117. if (a.get("direction") > 0) {
  118. a.save({ direction:-1 });
  119. this.model.set({
  120. positive_agreements_count:(this.model.get("positive_agreements_count") - 1),
  121. negative_agreements_count:(this.model.get("negative_agreements_count") + 1)
  122. });
  123. }
  124. } else if (this.should_create_agreement_model) {
  125. this.should_create_agreement_model = false;
  126. var ag = Agreements.create({ post_id:window._postID, event_id:window._eventID, comment_id:this.model.id, user_id:window._userID, direction:-1 });
  127. this.model.set({ negative_agreements_count:(this.model.get("negative_agreements_count") + 1) });
  128. }
  129. return false;
  130. },
  131. current_comment_agreement: function() {
  132. return Agreements.find(function(ag){
  133. return (ag.get("user_id") == window._userID) && (ag.get("comment_id") == this.model.id);
  134. }, this);
  135. }
  136. });
  137. // view: CommentApp
  138. window.CommentAppView = Backbone.View.extend({
  139. el: $("#comments_app"),
  140. events: {
  141. "submit form.new_comment" : "create"
  142. },
  143. initialize: function() {
  144. Comments.bind('add', this.addOne, this);
  145. Comments.bind('reset', this.addAll, this);
  146. // Comments.fetch();
  147. // Agreements.fetch();
  148. Comments.reset(window._commentsData);
  149. Agreements.reset(window._agreementsData);
  150. },
  151. create: function() {
  152. var input = $("form.new_comment textarea");
  153. var text = input.val();
  154. if (!text || text.length < 1) {
  155. alert("내용을 입력해주세요.");
  156. return false;
  157. }
  158. var c = Comments.create({ body: text, user_id: window._userID, post_id: window._postID, user:{name: window._userName} });
  159. input.val('');
  160. return false;
  161. },
  162. addOne: function(comment) {
  163. var view = new CommentView({ model: comment, id: "comment_" + (comment.id || comment.cid) });
  164. this.$("#comments").prepend(view.render().el);
  165. },
  166. addAll: function() {
  167. Comments.each(this.addOne);
  168. }
  169. });
  170. window.CommentApp = new window.CommentAppView;
  171. } // _CommentApp
  172. });