Advertisement
Guest User

02.Blog

a guest
Nov 21st, 2016
308
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. function attachEvents() {
  3.     // Закоментирах този ред и затварящите, защото в judge не трябва и гърми с тях
  4.     // $(document).ready(function () {
  5.         // Махнах наклонената черта от url-ла на постовете защото когато искаш да вземеш всички не трябва и гърми
  6.         let postsUrl = 'https://baas.kinvey.com/appdata/kid_BkyzcBAbe/posts';
  7.         let commentsUrl = 'https://baas.kinvey.com/appdata/kid_BkyzcBAbe/comments/';
  8.         let auth = {'Authorization': 'Basic ' + btoa('peter:p')};
  9.  
  10.         $('#btnLoadPosts').click(function () {
  11.             $.get({url: postsUrl, headers: auth}).then(displayPosts).catch(displayError);
  12.         });
  13.  
  14.         $('#btnViewPost').click(function () {
  15.             let selectedPostId = $('#posts').val();
  16.             if (!selectedPostId) return;
  17.            
  18.             // Тук добавих наклонената черта, защото трябва, когато се избира определен пост
  19.             let postsRequest = $.get({url: postsUrl + `/${selectedPostId}`, headers: auth});
  20.             let commentsRequest =  $.get({url: commentsUrl + `?query={"post_id":"${selectedPostId}"}`, headers: auth});
  21.  
  22.             Promise.all([postsRequest, commentsRequest]).then(displayPostInfo).catch(displayError);
  23.         });
  24.  
  25.         function displayPosts(posts) {
  26.             $('#posts').empty();
  27.  
  28.             for (let post of posts) {
  29.                 $('#posts').append($('<option>').text(post.title).val(post._id));
  30.             }
  31.         }
  32.  
  33.         function displayPostInfo([postInfo, comments]) {
  34.             $('#post-title').text(postInfo.title);
  35.             $('#post-body').text(postInfo.body);
  36.  
  37.             $('#post-comments').empty();
  38.  
  39.             for (let comment of comments) {
  40.                 $('#post-comments').append($('<li>').text(comment.text));
  41.             }
  42.         }
  43.  
  44.         function displayError(error) {
  45.             let errorDiv = $('<div>').text(`Error: ${error.status} (${error.statusText})`);
  46.             $(document.body).prepend(errorDiv);
  47.  
  48.             setTimeout(() => errorDiv.fadeOut(() => errorDiv.remove()), 3000);
  49.         }
  50.     // });
  51. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement