Advertisement
Guest User

Untitled

a guest
May 21st, 2019
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.63 KB | None | 0 0
  1. 'use strict';
  2.  
  3. const STORE = [
  4. {id: cuid(), name: "apples", checked: false},
  5. {id: cuid(), name: "oranges", checked: false},
  6. {id: cuid(), name: "milk", checked: true},
  7. {id: cuid(), name: "bread", checked: false}
  8. ];
  9.  
  10.  
  11. function generateItemElement(item) {
  12. return `
  13. <li data-item-id="${item.id}">
  14. <span class="shopping-item js-shopping-item ${item.checked ? "shopping-item__checked" : ''}">${item.name}</span>
  15. <div class="shopping-item-controls">
  16. <button class="shopping-item-toggle js-item-toggle">
  17. <span class="button-label">check</span>
  18. </button>
  19. <button class="shopping-item-delete js-item-delete">
  20. <span class="button-label">delete</span>
  21. </button>
  22. </div>
  23. </li>`;
  24. }
  25.  
  26.  
  27. function generateShoppingItemsString(shoppingList) {
  28. console.log("Generating shopping list element");
  29.  
  30. const items = shoppingList.map((item) => generateItemElement(item));
  31.  
  32. return items.join("");
  33. }
  34.  
  35.  
  36. function renderShoppingList() {
  37. // render the shopping list in the DOM
  38. console.log('`renderShoppingList` ran');
  39. const shoppingListItemsString = generateShoppingItemsString(STORE);
  40.  
  41. // insert that HTML into the DOM
  42. $('.js-shopping-list').html(shoppingListItemsString);
  43. }
  44.  
  45.  
  46. function handleNewItemSubmit() {
  47. // this function will be responsible for when users add a new shopping list item
  48.  
  49. //focus on the input and store the new entry into a variable
  50. //make a new list element for the submitted entry
  51. //insert the new list string in the DOM
  52. console.log('`handleNewItemSubmit` ran');
  53. }
  54.  
  55.  
  56. function handleItemCheckClicked() {
  57. // this function will be responsible for when users click the "check" button on
  58. // a shopping list item.
  59.  
  60. //listen for when the user clicks on the check button
  61. //change the item's checked state
  62. //render in the DOM
  63. console.log('`handleItemCheckClicked` ran');
  64. }
  65.  
  66.  
  67. function handleDeleteItemClicked() {
  68. // this function will be responsible for when users want to delete a shopping list
  69. // item
  70.  
  71. //listen for when the user clicks on the delete button
  72. //remove the item from the STORE
  73. //render in the DOM
  74. console.log('`handleDeleteItemClicked` ran')
  75. }
  76.  
  77. // this function will be our callback when the page loads. it's responsible for
  78. // initially rendering the shopping list, and activating our individual functions
  79. // that handle new item submission and user clicks on the "check" and "delete" buttons
  80. // for individual shopping list items.
  81. function handleShoppingList() {
  82. renderShoppingList();
  83. handleNewItemSubmit();
  84. handleItemCheckClicked();
  85. handleDeleteItemClicked();
  86.  
  87. }
  88.  
  89. // when the page loads, call `handleShoppingList`
  90. $(handleShoppingList);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement