Advertisement
Guest User

Untitled

a guest
Mar 30th, 2018
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.57 KB | None | 0 0
  1. function attachEvents() {
  2. const URL = 'https://baas.kinvey.com/appdata/kid_HJICDDTH/';
  3. const USERNAME = 'Peter';
  4. const PASSWORD = 'p';
  5. const BASE_64 = btoa(USERNAME + ':' + PASSWORD);
  6. const AUTH = {'Authorization': 'Basic ' + BASE_64};
  7. const POSTS = $('#posts');
  8. const POSTCOMMENTS = $('#post-comments');
  9. let posts = {};
  10.  
  11. $('#btnLoadPosts').on('click', getPosts);
  12. $('#btnViewPost').on('click', loadComments);
  13.  
  14. function getPosts() {
  15. $.ajax({
  16. method: 'GET',
  17. url: URL + 'posts',
  18. headers: AUTH
  19. }).then(appendPosts);
  20.  
  21. function appendPosts(res) {
  22. POSTS.empty();
  23. for (let post of res) {
  24. let newOption = $(`<option value="${post._id}">${post.title}</option>`);
  25. POSTS.append(newOption);
  26. posts[post._id] = post.body;
  27. }
  28. }
  29. }
  30.  
  31. function loadComments() {
  32. let postId = $('#posts').find(":selected")[0];
  33. $('#post-title').text(postId.textContent);
  34. $('#post-body').text(posts[postId.value]);
  35.  
  36. $.ajax({
  37. method: 'GET',
  38. url: URL + `comments/?query={"post_id":"${postId.value}"}`,
  39. headers: AUTH
  40. }).then(displayComments);
  41.  
  42. function displayComments(comments) {
  43. POSTCOMMENTS.empty();
  44. for (let comment of comments) {
  45. let currentComment = $(`<li>${comment.text}</li>`);
  46. POSTCOMMENTS.append(currentComment);
  47. }
  48. }
  49. }
  50. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement