Advertisement
Smytt

Untitled

Mar 30th, 2018
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.     function attachEvents() {
  2.         const URL = 'https://baas.kinvey.com/appdata/kid_S1VqfOscG/'
  3.         const USER = 'peter'
  4.         const PASS = 'p'
  5.         const BASE64 = btoa(`${USER}:${PASS}`)
  6.         const AUTH = {"Authorization": "Basic " + BASE64}
  7.         let posts = {}
  8.  
  9.         $('#btnLoadPosts').click(loadPosts)
  10.         $('#btnViewPost').click(viewPosts)
  11.  
  12.         function loadPosts() {
  13.             $('#posts').empty()
  14.             $.ajax({
  15.                 url: URL + 'posts/',
  16.                 headers: AUTH,
  17.             }).then((res) => {
  18.                 for (let post of res) {
  19.                     $('#posts')
  20.                         .append($(`<option value="${post._id}">`)
  21.                             .text(post.title))
  22.                     posts[post._id] = post
  23.                 }
  24.             }).catch((err) => {
  25.                 console.log(err);
  26.             })
  27.         }
  28.  
  29.         function viewPosts() {
  30.             $('#post-comments').empty()
  31.             let postId = $('#posts').find('option:selected').val()
  32.             $('#post-title').text(posts[postId].title)
  33.             $('#post-body').text(posts[postId].body)
  34.             $.ajax({
  35.                 url: URL + 'comments/',
  36.                 headers: AUTH,
  37.             }).then((res) => {
  38.                 for (let comment of res) {
  39.                     if (comment.post_id === postId) {
  40.                         $('#post-comments')
  41.                             .append($('<li>').text(comment.text))
  42.                     }
  43.                 }
  44.             }).catch((err) => {
  45.                 console.log(err);
  46.             })
  47.         }
  48.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement