Advertisement
Guest User

comments.js

a guest
Dec 4th, 2015
161
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. $(function initializeCommentComponents() {
  2.  
  3.     $(document).on('click', '.show-comments', function (evt) {
  4.         evt.stopPropagation();
  5.         new Post(this).showComments();
  6.         return false;
  7.     });
  8.  
  9.     $(document).on('click', '.add-comment', function (evt) {
  10.         evt.stopPropagation();
  11.         new Post(this).showAddComment();
  12.         return false;
  13.     });
  14.  
  15.     $(document).on('submit', '.new-comment form', function (evt) {
  16.         evt.stopPropagation();
  17.         new Post(this).addComment();
  18.         return false;
  19.     });
  20. });
  21.  
  22. /*  Post class as an object-oriented wrapper around DOM elements */
  23. function Post(el) {
  24.  
  25.     var $el = $(el),
  26.         postEl = $el.hasClass('blog-post') ? $el : $el.parents('.blog-post'),
  27.         postId = postEl.data('post-id'),
  28.         addCommentEl = postEl.find('.add-comment'),
  29.         newCommentEl = postEl.find('.new-comment'),
  30.         commentEl = newCommentEl.find('[name=Body]'),
  31.         commentsContainer = postEl.find('.comments-container'),
  32.         commentsEl = postEl.find('.comments'),
  33.         showCommentsButton = postEl.find('.show-comments'),
  34.         noCommentsEl = postEl.find('.no-comments');
  35.  
  36.  
  37.     return {
  38.         addComment: addComment,
  39.         renderComment: renderComments,
  40.         showAddComment: showAddComment,
  41.         showComments: showComments,
  42.     };
  43.  
  44.  
  45.     /*********  Public methods ****************/
  46.     function addComment() {
  47.         var comment = {
  48.             PostId: postId,
  49.             Body: commentEl.val(),
  50.         };
  51.         PostCommentService.addComment(comment).then(renderComments);
  52.     }
  53.  
  54.     function showAddComment() {
  55.         addCommentEl.addClass('hide');
  56.         newCommentEl.removeClass('hide');
  57.         commentEl.focus();
  58.     }
  59.  
  60.     function showComments() {
  61.         PostCommentService.getComments(postId).then(renderComments);
  62.     }
  63.  
  64.  
  65.  
  66.     /*********  Private methods ****************/
  67.     function createCommentElements(comments) {
  68.         comments = [].concat(comments);
  69.  
  70.         if (!comments.length) {
  71.             return $('<div class="no-comments">No comments</div>');
  72.         }
  73.  
  74.         return comments.reduce(function (commentEls, comment) {
  75.             var el =
  76.                 $('<div class="comment">')
  77.                     .data('comment-id', comment.Id)
  78.                     .append($('<p class="details">').append(comment.Author || 'Anon').append(' at ' + new Date(comment.PostedDate).toLocaleString()))
  79.                     .append($('<p class="body">').append(comment.Body));
  80.  
  81.             return commentEls.concat(el);
  82.         }, []);
  83.     }
  84.  
  85.     function renderComments(comments) {
  86.         var commentEls = createCommentElements(comments);
  87.         commentsEl.append(commentEls);
  88.         commentsContainer.removeClass('hide');
  89.         showCommentsButton.remove();
  90.         noCommentsEl.remove();
  91.         resetAddComment();
  92.     }
  93.  
  94.     function resetAddComment() {
  95.         addCommentEl.removeClass('hide');
  96.         newCommentEl.addClass('hide');
  97.         commentEl.val('');
  98.     }
  99. }
  100.  
  101.  
  102.  
  103. var PostCommentService = (
  104.     function PostCommentService() {
  105.  
  106.         function call(postId, method, data) {
  107.             return $.ajax({
  108.                 // RESTful Web API URL:  /api/posts/{postId}/comments
  109.                 url: ['/api/posts', postId, 'comments'].join('/'),
  110.                 type: method,
  111.                 data: JSON.stringify(data),
  112.                 contentType: 'application/json'
  113.             });
  114.         }
  115.  
  116.         return {
  117.  
  118.             // Add comment by calling URL with POST method and passing data
  119.             addComment: function (comment) {
  120.                 return call(comment.PostId, 'POST', comment);
  121.             },
  122.  
  123.             // Get comments by calling URL with GET method
  124.             getComments: function (postId) {
  125.                 return call(postId, 'GET');
  126.             }
  127.         };
  128.     })();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement