Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function attachEvents() {
- const URL = 'https://baas.kinvey.com/appdata/kid_S1VqfOscG/'
- const USER = 'peter'
- const PASS = 'p'
- const BASE64 = btoa(`${USER}:${PASS}`)
- const AUTH = {"Authorization": "Basic " + BASE64}
- let posts = {}
- $('#btnLoadPosts').click(loadPosts)
- $('#btnViewPost').click(viewPosts)
- function loadPosts() {
- $('#posts').empty()
- $.ajax({
- url: URL + 'posts/',
- headers: AUTH,
- }).then((res) => {
- for (let post of res) {
- $('#posts')
- .append($(`<option value="${post._id}">`)
- .text(post.title))
- posts[post._id] = post
- }
- }).catch((err) => {
- console.log(err);
- })
- }
- function viewPosts() {
- $('#post-comments').empty()
- let postId = $('#posts').find('option:selected').val()
- $('#post-title').text(posts[postId].title)
- $('#post-body').text(posts[postId].body)
- $.ajax({
- url: URL + 'comments/',
- headers: AUTH,
- }).then((res) => {
- for (let comment of res) {
- if (comment.post_id === postId) {
- $('#post-comments')
- .append($('<li>').text(comment.text))
- }
- }
- }).catch((err) => {
- console.log(err);
- })
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement