Advertisement
Guest User

Untitled

a guest
Sep 17th, 2019
156
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /**
  2.  ** This an ES5 version attempt at the code shown in the tutorial.
  3.  ** tutorial uses classes, static methods and the DOM to manupilate
  4.  ** events and UI tasks.
  5.  */
  6.  
  7. /**
  8.  * TODO: Create a book object
  9.  * ? represents a book
  10.  */
  11.  
  12. //Book constructor
  13. const book = (title, author, isbn) => {
  14.   this.title = title;
  15.   this.author = author;
  16.   this.isbn = isbn;
  17. };
  18.  
  19. /**
  20.  * TODO: UI tasks
  21.  * ? handles UI tasks
  22.  */
  23.  
  24. //array of stored books this method loops through the list
  25. const displayBooks = () => {
  26.   const storedBook = [
  27.     {
  28.       title: "Book One",
  29.       author: "John Doe",
  30.       isbn: "334"
  31.     },
  32.     {
  33.       title: "Book two",
  34.       author: "John Doe",
  35.       isbn: "734"
  36.     }
  37.   ];
  38.  
  39.   //books variable that holds the list of books
  40.   const books = storedBook;
  41.  
  42.   //looping through the books array and calling addBookToList on each books
  43.   books.forEach(book => addBookToList(book));
  44. };
  45.  
  46. //This methods shows all the books from storedbooks on the UI
  47. const addBookToList = book => {
  48.   //grab this the table body element from the dom
  49.   const list = document.querySelector("#book-list");
  50.  
  51.   //DOM creates a table row
  52.   const row = document.createElement("tr");
  53.  
  54.   //DOM adds the colums to the row with the book variables
  55.   row.innerHTML = `  
  56.     <td>${book.title}</td>
  57.     <td>${book.author}</td>
  58.     <td>${book.isbn}</td>
  59.     <td><a href="#" class="btn btn-danger btn-sm delete">X</a></td>
  60.     `;
  61.   //DOM adds the row to the table body element
  62.   list.appendChild(row);
  63. };
  64.  
  65. /**
  66.  * TODO: Storage tasks
  67.  * ? handles storage
  68.  */
  69.  
  70. /**
  71.  * TODO: Event: Display books
  72.  * ? show book in the list
  73.  */
  74.  
  75. //displays books in storedBooks by looping and calling the addBookToList on each one
  76. document.addEventListener("DOMcontentLoaded", displayBooks());
  77.  
  78. /**
  79.  * TODO: Event: Add a book
  80.  * ? adds a book to storage by colecting data from the form and instanciating a book
  81.  */
  82. const addedBook = new book(title, author, isbn);
  83.  
  84. document.querySelector("#book-form").addEventListener("submit", e => {
  85.   //Prevent actual submit
  86.   e.preventDefault();
  87.  
  88.   //Get form values
  89.   const title = document.querySelector("#title").value;
  90.   const author = document.querySelector("#author").value;
  91.   const isbn = document.querySelector("#isbn").value;
  92.  
  93.     //Instantiates a book
  94.     addedBook(title, author, isbn) = {
  95.         title: this.title,
  96.         author: this.author,
  97.         isbn: this.isbn
  98.     };
  99.  
  100.   console.log(addedBook);
  101. });
  102.  
  103. /**
  104.  * TODO: Event: Remove a book
  105.  * ? removes a book from the UI(list) and storage
  106.  */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement