Advertisement
kema

Countries & Towns

Nov 29th, 2016
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function attachEvents() {
  2.  
  3.     const kinveyAppId = "kid_SySlm5uGg";
  4.     const serviceUrl="https://baas.kinvey.com/appdata/" + kinveyAppId;
  5.     const kinveyUsername="kem";
  6.     const kinveyPassword="kem";
  7.     const base64auth=btoa(kinveyUsername+":"+kinveyPassword);
  8.     const authHeaders = {"Authorization": "Basic "+ base64auth};
  9.  
  10.  
  11.  
  12.     function showView(viewName) {
  13.         $('main > section').hide();
  14.         $('#' + viewName).show()
  15.     }
  16.  
  17.     function loadCountriesClicked() {
  18.         $('#countries').empty();
  19.         $.ajax({
  20.             method: "GET",
  21.             url: serviceUrl + "/countries",
  22.             headers: authHeaders,
  23.             success: displayCountries,
  24.             error: handleAjaxError
  25.         });
  26.         function displayCountries(countries) {
  27.             let table = $(
  28.                 `<table>
  29.                 <tr>
  30.                     <th>Country</th>
  31.                     <th>Actions</th>
  32.                 </tr>
  33.             </table>`);
  34.  
  35.             for (let country of countries) {
  36.                 let tr = $('<tr>');
  37.                 displayTableRow(tr, country);
  38.                 tr.appendTo(table);
  39.             }
  40.  
  41.             $('#countries').append(table);
  42.             $('#countries').append($("<input type='text' name='countryName'/>"));
  43.             $('#countries').append($("<a href='#'>[Add Country]</a>").click(createContry));
  44.  
  45.  
  46.             function displayTableRow(tr, country) {
  47.                 let links = [];
  48.                 let viewTowns = $("<a href='#'>[View towns]</a>").click(function () {
  49.                     showTowns(country._id)
  50.                 });
  51.  
  52.                 let deleteLink = $("<a href='#'>[Delete]</a>").click(function () {
  53.                     deleteTownsInCountry(country._id)
  54.                 });
  55.                 let editLink = $("<a href='#'>[Edit]</a>")
  56.                 //     .click(function () {
  57.                 //     loadCountry(ForEdit(country._id)
  58.                 // });
  59.                 links.push(viewTowns);
  60.                 links.push(' ');
  61.                 links.push(deleteLink);
  62.                 links.push(' ');
  63.                 links.push(editLink);
  64.  
  65.                 tr.append(
  66.                     $("<td>").text(country.name),
  67.                     $("<td>").append(links)
  68.                 )
  69.             }
  70.         }
  71.     }
  72.  
  73.     function showTowns(countryId) {
  74.         $('#formShowTowns').empty();
  75.         showView('viewTowns');
  76.         $.ajax({
  77.             method: "GET",
  78.             url: serviceUrl + `/towns/?query={"country":"${countryId}"}`,
  79.             headers: authHeaders,
  80.             success: loadViewTownsSuccess,
  81.             error: handleAjaxError
  82.         });
  83.  
  84.         function loadViewTownsSuccess(towns) {
  85.             let table = $(
  86.                 `<table>
  87.                 <tr>
  88.                     <th>Town</th>
  89.                     <th>Actions</th>
  90.                 </tr>
  91.             </table>`);
  92.             for( town of towns){
  93.                 let tr = $('<tr>');
  94.                 displayTownInTable(tr, town, countryId);
  95.                 tr.appendTo(table);
  96.             }
  97.             $('#formShowTowns').append(table);
  98.             $('#formShowTowns').append($("<input type='text' name='townName'/>"));
  99.             $('#formShowTowns').append($("<a href='#'>[Add Town]</a>").click(function(){
  100.                 createTown(countryId)
  101.             }));
  102.  
  103.             function displayTownInTable(tr, town, countryId){
  104.                 let links = [];
  105.                 let deleteLink = $("<a href='#'>[Delete]</a>").click(function () {
  106.                     deleteTown(town._id, countryId)
  107.                 });
  108.                 let editLink = $("<a href='#'>[Edit]</a>").click(function () {
  109.                     loadTownForEdit(town)
  110.                 });
  111.                 links.push(deleteLink);
  112.                 links.push(' ');
  113.                 links.push(editLink);
  114.  
  115.                 tr.append(
  116.                     $("<td>").text(town.name),
  117.                     $("<td>").append(links)
  118.                 )
  119.             }
  120.  
  121.         }
  122.     }
  123.  
  124.  
  125.     loadCountriesClicked();
  126.  
  127.     function createContry() {
  128.         let adCountry = {
  129.             name: $('#countries input[name=countryName]').val()
  130.         };
  131.         if ($('#countries input[name=countryName]').val() != '') {
  132.             $.ajax({
  133.                 method: "POST",
  134.                 url: serviceUrl + "/countries",
  135.                 headers: authHeaders,
  136.                 data: JSON.stringify(adCountry),
  137.                 contentType: "application/json",
  138.                 success: loadCountriesClicked,
  139.                 error: handleAjaxError
  140.             });
  141.  
  142.         }
  143.     }
  144.  
  145.     function createTown(countryID) {
  146.         let adTown = {
  147.             name: $('#formShowTowns input[name=townName]').val(),
  148.             country: countryID
  149.         };
  150.         if ($('#formShowTowns input[name=townName]').val()!=''){
  151.             $.ajax({
  152.                 method: "POST",
  153.                 url: serviceUrl + `/towns/?query={"country":"${countryID}"}`,
  154.                 headers: authHeaders,
  155.                 data: JSON.stringify(adTown),
  156.                 contentType: "application/json",
  157.                 success: function () {
  158.                     showTowns(countryID)
  159.                 },
  160.                 error: handleAjaxError
  161.             });
  162.         }
  163.  
  164.     }
  165.     function deleteTownsInCountry(countryID) {
  166.         $.ajax({
  167.             method: "DELETE",
  168.             url: serviceUrl + `/towns/?query={"country":"${countryID}"}`,
  169.             headers: authHeaders,
  170.             success: function(){
  171.                 deleteCountry(countryID)
  172.             },
  173.             error: handleAjaxError
  174.         });
  175.  
  176.         function deleteCountry(countryID) {
  177.             $.ajax({
  178.                 method: "DELETE",
  179.                 url: serviceUrl + "/countries/"+countryID,
  180.                 headers: authHeaders,
  181.                 success: loadCountriesClicked,
  182.                 error: handleAjaxError
  183.             });
  184.         }
  185.     }
  186.     function deleteTown(townId, countryID) {
  187.         $.ajax({
  188.             method: "DELETE",
  189.             url: serviceUrl + "/towns/"+townId,
  190.             headers: authHeaders,
  191.             success: function () {
  192.                 showTowns(countryID)
  193.             },
  194.             error: handleAjaxError
  195.         });
  196.     }
  197.  
  198.     function loadTownForEdit(town) {
  199.         $('#formEditTown').empty();
  200.         showView('editTown');
  201.         $.ajax({
  202.             method: "GET",
  203.             url: serviceUrl + '/towns/'+town._id,
  204.             headers: authHeaders,
  205.             success: loadTownForEditSuccess,
  206.             error: handleAjaxError
  207.         });
  208.  
  209.  
  210.         function loadTownForEditSuccess(data) {
  211.             $('#formEditTown').append($('<input type="text" name="townName"/>').val(data.name));
  212.  
  213.             console.dir( $('#formEditAd input[name=townName]').val())
  214.             $('#formEditTown').append($('<button id="submitTown">Edit</button>').click(function () {
  215.                 editTown(data)}));
  216.         }
  217.     }
  218.  
  219.     function editTown(data) {
  220.         let adData = {
  221.             name: $('#formEditAd input[name=townName]').val(),
  222.             country: data.country
  223.         }
  224.         $.ajax({
  225.             method: "PUT",
  226.             url: serviceUrl + '/towns/'+ data._id,
  227.             headers: authHeaders,
  228.             data: JSON.stringify(adData),
  229.             contentType: "application/json",
  230.             success: function(){
  231.                 showTowns(data.country)
  232.             },
  233.             error: handleAjaxError
  234.         });
  235.  
  236.     }
  237.  
  238.     function handleAjaxError(response) {
  239.         let errorMsg = JSON.stringify(response);
  240.         if (response.readyState === 0)
  241.             errorMsg = "Cannot connect due to network error.";
  242.         if (response.responseJSON &&
  243.             response.responseJSON.description)
  244.             errorMsg = response.responseJSON.description;
  245.         showError(errorMsg);
  246.     }
  247.  
  248.     function showError(errorMsg) {
  249.         $('#errorBox').text("Error: " + errorMsg);
  250.         $('#errorBox').show();
  251.     }
  252. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement