Advertisement
sanjiisan

Untitled

Jun 6th, 2017
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
jQuery 5.31 KB | None | 0 0
  1. $(function () {
  2.     //Zmienne początkowe
  3.     var $repertuarList = $('.repertuar');
  4.     var apiUrl = 'http://localhost:3000/movies';
  5.  
  6.     //Obsługa edycji
  7.     $('body').on('click', '.edit', function () {
  8.         //Wiersz który kliknąłem
  9.         var $liElement = $(this).parent();
  10.         //Id klikniętego wiersza
  11.         var clickedId = $liElement.data('id');
  12.  
  13.         //Pobieramy treść tytułu i opisu z H3 i P wewnątrzy klikniętego wiersza
  14.         var oldTitle = $liElement.find('h3').text();
  15.         var oldDescription = $liElement.find('p').text();
  16.  
  17.         //Dodaje klasę editable która chowa elementy wyświetlające tytuł i opis
  18.         $liElement.addClass('editable');
  19.  
  20.         //Tworze inputy do ktorych wpisuje wartości pobrane z tytułu i opisu dla filmu
  21.         var $titleInput = $('<input>');
  22.         $titleInput.val(oldTitle);
  23.  
  24.         //Tworze inputy do ktorych wpisuje wartości pobrane z tytułu i opisu dla filmu
  25.         var $descriptionInput = $('<input>');
  26.         $descriptionInput.val(oldDescription);
  27.  
  28.         //Tworze przycisk który będzie zapisywał dane!
  29.         var $submitButton = $('<button>');
  30.         $submitButton.text('Zapisz');
  31.  
  32.         //Appenduję wszystkie dzieci
  33.         $liElement.append($titleInput);
  34.         $liElement.append($descriptionInput);
  35.         $liElement.append($submitButton);
  36.  
  37.         //Dodaje obsługę kliknięcia w przycisk zapisz
  38.         $submitButton.on('click', function () {
  39.             //Pobieram wartości z elementów INPUT które chwilę temu dodałem do całego LI
  40.             var title = $titleInput.val();
  41.             var description = $descriptionInput.val();
  42.  
  43.             //Tworzę obiekt który będzie edytował dane na serwerze
  44.             var editMovie = {
  45.                 description: description,
  46.                 title: title
  47.             };
  48.  
  49.             //Edycja danych na serwerze
  50.             $.ajax({
  51.                 type: "PUT",
  52.                 url: apiUrl + '/' + clickedId, //podaje ścieżkę do /movies/{id} - id klikniętego wiersza
  53.                 data: editMovie,
  54.                 dataType: 'json'
  55.             })
  56.                 .done(function (response) {
  57.                     //Kiedy dane na serwerze się zapdejtują to usuwam klasę która robi obramowanie
  58.                     $liElement.removeClass('editable');
  59.  
  60.                     //Usuwam wszystkie dodatkowe przyciski
  61.                     $titleInput.remove();
  62.                     $descriptionInput.remove();
  63.                     $submitButton.remove();
  64.  
  65.                     //Wpisuję nowe wartości do tytułu i opisu
  66.                     $liElement.find('h3').text(response.title);
  67.                     $liElement.find('p').text(response.description);
  68.                 })
  69.                 .fail(function (error) {
  70.                     console.log('Error!', error);
  71.                 });
  72.         });
  73.     });
  74.  
  75.     //Obsługa kliknięcia DELETE(przyciski delete sa generowane dynamicznie)
  76.     $('body').on('click', '.delete', function () {
  77.         //Pobieramy element LI - który jest rodzicem przycisku do usuwania
  78.         var $parent = $(this).parent();
  79.         var clickedLiId = $parent.data('id'); //pobieramy ID kliknietego LI
  80.  
  81.         //Usuwanie danych z bazy - za pomoca metody DELETE
  82.         $.ajax({
  83.             type: "DELETE",
  84.             url: apiUrl + '/' + clickedLiId, //Adres do usuwanie zasobow wygląda tak: http://localhost:3000/movies/{id} - to id na koncu doklejamy do string'a
  85.             dataType: 'json'
  86.         })
  87.             .done(function (response) {
  88.                 //kiedy usunelismy rzeczy z bazy danych - usuwamy cały element LI z html'a
  89.                 $parent.remove();
  90.             })
  91.             .fail(function (error) {
  92.                 console.log('Error!', error);
  93.             });
  94.     });
  95.  
  96.     //Dodawanie nowych filmów
  97.     $('.add_movie').on('submit', function (event) {
  98.         //Domyślnie event submit robi przeladowanie strony - stąd robimy event.preventDefault();
  99.         event.preventDefault();
  100.  
  101.         //Pobieramy wartości z inputów
  102.         var title = $('.get_title').val();
  103.         var description = $('.get_description').val();
  104.  
  105.         //Tworzymy nowy obiekt filmu który będziemy wysyłać na serwer
  106.         var newMovie = {
  107.             title: title,
  108.             description: description
  109.         };
  110.  
  111.         $.ajax({
  112.             type: "POST", //POST - bo tworzymy nowe zasób
  113.             url: apiUrl,
  114.             data: newMovie, //W data wysyłamy te dane które mają być nowym filmem
  115.             dataType: 'json'
  116.         })
  117.             .done(function (response) {
  118.                 renderMovie(response); //Użycie tej samej funkcji który na podstawie obiektu filmy wygeneruje nowy wiersz z nowym filmem
  119.             })
  120.             .fail(function (error) {
  121.                 console.log('Error!', error);
  122.             });
  123.     });
  124.  
  125.     //Funkcja na podstawie obiektu filmu(argument movie) renderuje(tworzy kod HTML) który przedstawia zadany film
  126.     function renderMovie(movie) {
  127.         var $newListElement = $('<li>');
  128.  
  129.         var $title = $('<h3>');
  130.         $title.text(movie.title);
  131.  
  132.         var $desc = $('<p>');
  133.         $desc.text(movie.description);
  134.  
  135.         var $editBtn = $("<button>", {class: "edit"});
  136.         $editBtn.text('Edytuj');
  137.  
  138.         var $removeBtn = $("<button>", {class: "delete"});
  139.         $removeBtn.text('Usuń');
  140.  
  141.         //W każdym elemencie <LI> zapisujemy w DATA_SECIE id konkretnego filmu
  142.         $newListElement.data('id', movie.id);
  143.  
  144.         //Appendujemy wszystko ok LI
  145.         $newListElement.append($title);
  146.         $newListElement.append($desc);
  147.         $newListElement.append($removeBtn);
  148.         $newListElement.append($editBtn);
  149.  
  150.         //Appenduje LI do naszej listy UL
  151.         $repertuarList.append($newListElement);
  152.     }
  153.  
  154.     //Funkcja początkowa ładująca wszystkie filmy
  155.     function loadMovies() {
  156.         //dane sa ładowane ajaxem
  157.         $.ajax({
  158.             type: "GET",
  159.             url: apiUrl,
  160.             dataType: 'json'
  161.         })
  162.             .done(function (response) {
  163.                 for (var i = 0; i < response.length; i++) {
  164.                     //pod zmienną resposne[i] mam pojedynczy obiekt który jest filmem
  165.                     renderMovie(response[i]);
  166.                 }
  167.             })
  168.             .fail(function (error) {
  169.                 console.log('Error!', error);
  170.             });
  171.     }
  172.  
  173.     loadMovies();
  174. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement