Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function attachEvents() {
- $(document).ready(function() {
- const kinveykinveyAppID = "kid_B15S6I9Zl";
- const serviceUrl = "https://baas.kinvey.com/appdata/" + kinveykinveyAppID;
- const kinveyUsername = "peter";
- const kinveyPassword = "p";
- const base64auth = btoa(kinveyUsername + ":" + kinveyPassword);
- const authHeaders = { "Authorization": "Basic " + base64auth };
- $("#btnLoadPosts").click(loadPostsClick);
- $("#btnViewPost").click(viewPostClick);
- function loadPostsClick() {
- let loadPostsRequest = {
- url: serviceUrl + "/posts",
- headers: authHeaders
- };
- $.ajax(loadPostsRequest)
- .then(displayPosts)
- .catch(displayError);
- }
- function displayPosts(posts) {
- $("#posts").empty();
- for (let post of posts) {
- let option = $("<option>")
- .text(post.title)
- .val(post._id);
- $("#posts").append(option);
- }
- }
- function displayError(err) {
- let errorDiv = $("<div>").text("Error: " + err.status + ' (' + err.statusText + ')');
- $(document.body).prepend(errorDiv);
- setTimeout(function() {
- $(errorDiv).fadeOut(function() {
- $(errorDiv).remove();
- });
- }, 3000);
- }
- function viewPostClick() {
- let selectedPostId = $("#posts").val();
- if (!selectedPostId) return;
- let requestPosts = $.ajax({
- url: serviceUrl + "/posts/" + selectedPostId,
- headers: authHeaders });
- let requestComments = $.ajax({ url: serviceUrl + `/comments/?query={"post_id":"${selectedPostId}"}`,
- headers: authHeaders });
- Promise.all([requestPosts, requestComments])
- .then(displayPostWithComments)
- .catch(displayError);
- }
- function displayPostWithComments([post, comments]) {
- $("#post-title").text(post.title);
- $("#post-body").text(post.body);
- $("#post-comments").empty();
- for (let comment of comments) {
- let commentItem = $("<li>")
- .text(comment.text);
- $("#post-comments")
- .append(commentItem);
- }
- }
- });
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement