Advertisement
Guest User

Untitled

a guest
May 16th, 2021
26
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.43 KB | None | 0 0
  1. // Create a variable for the API endpoint. In this example, we're accessing Xano's API
  2. let xanoUrl = new URL('https://x8ki-letl-twmt.n7.xano.io/api:SM7Tzr-Z/');
  3.  
  4.  
  5. // Define a function (set of operations) to get restaurant information.
  6. // This will use the GET request on the URL endpoint
  7. function getBooks() {
  8.  
  9. // Create a request variable and assign a new XMLHttpRequest object to it.
  10. // XMLHttpRequest is the standard way you access an API in plain Javascript.
  11. let request = new XMLHttpRequest();
  12.  
  13. // Define a function (set of operations) to get restaurant information.
  14. // Creates a variable that will take the URL from above and makes sure it displays as a string.
  15. // We then add the word 'restaurant" so the API endpoint becomes https://x715-fe9c-6426.n7.xano.io/api:Iw1iInWB/restaurant
  16. let url = xanoUrl.toString() + 'books';
  17.  
  18.  
  19. // Remember the 'request' was defined above as the standard way to access an API in Javascript.
  20. // GET is the verb we're using to GET data from Xano
  21. request.open('GET', url, true)
  22.  
  23. // When the 'request' or API request loads, do the following...
  24. request.onload = function() {
  25.  
  26. // Store what we get back from the Xano API as a variable called 'data' and converts it to a javascript object
  27. let data = JSON.parse(this.response)
  28.  
  29. // Status 200 = Success. Status 400 = Problem. This says if it's successful and no problems, then execute
  30. if (request.status >= 200 && request.status < 400) {
  31.  
  32. // Map a variable called cardContainer to the Webflow element called "Cards-Container"
  33. const cardContainer = document.getElementById("people-grid")
  34.  
  35. // This is called a For Loop. This goes through each object being passed back from the Xano API and does something.
  36. // Specifically, it says "For every element in Data (response from API), call each individual item restaurant"
  37. data.forEach(books => {
  38.  
  39. // For each restaurant, create a div called card and style with the "Sample Card" class
  40. const style = document.getElementById('people-card')
  41. // Copy the card and it's style
  42. const card = style.cloneNode(true)
  43.  
  44. card.setAttribute('id', '');
  45. card.style.display = 'block';
  46.  
  47. // When a restuarant card is clicked, navigate to the item page by passing the restaurant id
  48. card.addEventListener('click', function() {
  49. document.location.href = "/item?id=" + books.id;
  50. });
  51.  
  52. // For each restaurant, Create an image and use the restaurant image coming from the API
  53. const img = card.getElementsByTagName('IMG')[0]
  54. img.src = books.cover.url + "?tpl=big:box"; // using Xano's template engine to re-size the pictures down and make them a box
  55.  
  56. // For each restaurant, create an h3 and set the text content to the restaurant's title
  57. const h3 = card.getElementsByTagName('H3')[0]
  58. h3.textContent = books.name;
  59.  
  60. // Place the card into the div "Cards-Container"
  61.  
  62. cardContainer.appendChild(card);
  63. })
  64. }
  65. }
  66.  
  67. // Send Restaurant request to API
  68. request.send();
  69. }
  70.  
  71.  
  72.  
  73. // This fires all of the defined functions when the document is "ready" or loaded
  74. (function() {
  75. getBooks();
  76. })();
  77.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement