Advertisement
fbinnzhivko

Untitled

Nov 17th, 2016
170
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4.     <meta charset="UTF-8">
  5.     <title>Title</title>
  6. </head>
  7. <body>
  8. <div id="text">
  9.     <h1>Phonebook</h1>
  10.     <button id="btnLoad">Load</button>
  11.     <ul id="phonebook"></ul>
  12.     <h1>Create</h1>
  13.     Person: <input type="text" id="person"><br>
  14.     Phone: <input type="text" id="phone"><br>
  15.     <button id="btnCreate">Create</button>
  16. </div>
  17. <script type="text/javascript" src="http://code.jquery.com/jquery-latest.min.js"></script>
  18. <script src="http://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>
  19. <script>
  20.  
  21.     $(function attachEvents() {
  22.         $('#btnLoad').click(loadContacts);
  23.         $('#btnCreate').click(createContacts);
  24.  
  25.         const baseURL = "https://phonebook-nakov.firebaseio.com/phonebook";
  26.  
  27.         function loadContacts() {
  28.             $('#phonebook').empty();
  29.             $.get(baseURL + ".json")
  30.                     .then(displayContacts)
  31.                     .catch(displayError)
  32.         }
  33.  
  34.         function displayContacts(contacts) {
  35.             let keys = Object.keys(contacts);
  36.             for (let key of keys) {
  37.                 let contact = contacts[key];
  38.                 let li = $("<li>");
  39.                 li.text(contact.person + ' ' + contact.phone + " ");
  40.                 li.appendTo($('#phonebook'));
  41.                 li.append(
  42.                         $("<a href='#'>[Delete]</a>")
  43.                                 .click(function () {
  44.                                     deleteContact(key);
  45.                                 }))
  46.             }
  47.  
  48.         }
  49.  
  50.         function displayError() {
  51.             $('#phonebook').html("<li>Error</li>")
  52.         }
  53.  
  54.         function deleteContact(key) {
  55.             let delRequest = {
  56.                 method: "DELETE",
  57.                 url: baseURL + "/" + key + ".json"
  58.  
  59.             };
  60.             $.ajax(delRequest)
  61.                     .then(loadContacts)
  62.                     .catch(displayError);
  63.         }
  64.  
  65.         function createContacts() {
  66.             let person = $('#person').val();
  67.             let phone = $('#phone').val();
  68.             let newContact = {person, phone};
  69.  
  70.             let createrequest = {
  71.                 method: "POST",
  72.                 url: baseURL + ".json",
  73.                 data: JSON.stringify(newContact)
  74.  
  75.             };
  76.             $.ajax(createrequest)
  77.                     .then(loadContacts)
  78.                     .catch(displayError);
  79.         }
  80.  
  81.     });
  82.  
  83.  
  84. </script>
  85.  
  86. </body>
  87. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement