Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function attachEvents() {
- // Закоментирах този ред и затварящите, защото в judge не трябва и гърми с тях
- // $(document).ready(function () {
- // Махнах наклонената черта от url-ла на постовете защото когато искаш да вземеш всички не трябва и гърми
- let postsUrl = 'https://baas.kinvey.com/appdata/kid_BkyzcBAbe/posts';
- let commentsUrl = 'https://baas.kinvey.com/appdata/kid_BkyzcBAbe/comments/';
- let auth = {'Authorization': 'Basic ' + btoa('peter:p')};
- $('#btnLoadPosts').click(function () {
- $.get({url: postsUrl, headers: auth}).then(displayPosts).catch(displayError);
- });
- $('#btnViewPost').click(function () {
- let selectedPostId = $('#posts').val();
- if (!selectedPostId) return;
- // Тук добавих наклонената черта, защото трябва, когато се избира определен пост
- let postsRequest = $.get({url: postsUrl + `/${selectedPostId}`, headers: auth});
- let commentsRequest = $.get({url: commentsUrl + `?query={"post_id":"${selectedPostId}"}`, headers: auth});
- Promise.all([postsRequest, commentsRequest]).then(displayPostInfo).catch(displayError);
- });
- function displayPosts(posts) {
- $('#posts').empty();
- for (let post of posts) {
- $('#posts').append($('<option>').text(post.title).val(post._id));
- }
- }
- function displayPostInfo([postInfo, comments]) {
- $('#post-title').text(postInfo.title);
- $('#post-body').text(postInfo.body);
- $('#post-comments').empty();
- for (let comment of comments) {
- $('#post-comments').append($('<li>').text(comment.text));
- }
- }
- function displayError(error) {
- let errorDiv = $('<div>').text(`Error: ${error.status} (${error.statusText})`);
- $(document.body).prepend(errorDiv);
- setTimeout(() => errorDiv.fadeOut(() => errorDiv.remove()), 3000);
- }
- // });
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement