Guest User

Untitled

a guest
Nov 22nd, 2016
26
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.96 KB | None | 0 0
  1. function attachEvents() {
  2. const kinveyAppId = "kid_rkKP5QGfe";
  3. const serviceUrl = "https://baas.kinvey.com/appdata/" + kinveyAppId;
  4. const kinveyUsername = "guest";
  5. const kinveyPassword = "guest";
  6. const base64auth = btoa(kinveyUsername + ":" + kinveyPassword);
  7. const authHeaders = {
  8. "Authorization": "Basic " + base64auth,
  9. "Content-type": "application/json"
  10. };
  11. $("#addButton").on('click', addCountry);
  12. $("#load").on('click', loadCountries);
  13. function addCountry() {
  14. console.log('check')
  15. let name = $("#addCountry").val();
  16. let obj = {
  17. name: name
  18. };
  19. let request = {
  20. method: "POST",
  21. url: serviceUrl + "/Country",
  22. data: JSON.stringify(obj),
  23. headers: authHeaders
  24. };
  25. $.ajax(request)
  26. .then(loadCountries)
  27. .catch(displayError);
  28. }
  29. function loadCountries() {
  30. let request = {
  31. method: "GET",
  32. url: serviceUrl + "/Country",
  33. headers: authHeaders
  34. };
  35. $.ajax(request)
  36. .then(displayCountries)
  37. .catch(displayError)
  38. }
  39. function displayCountries(data) {
  40. for(let country of data){
  41. let id = country._id;
  42. let tr = $("<tr>").attr('data-id', id);
  43. let nameTd = $("<td>").text(country.name).appendTo($(tr));
  44. let actionsTd = $("<td>");
  45. let editBut = $("<input type='button' id='editBut' value='Edit'>").appendTo($(actionsTd));
  46. let deleteBut = $("<input type='button' id='deleteBut' value='Delete'/>").appendTo($(actionsTd));
  47. $(actionsTd).appendTo($(tr));
  48. $(tr).appendTo($("#countries"));
  49. }
  50. }
  51. $("#deleteBut").click(deleteCountry);
  52. function deleteCountry() {
  53. console.log(5)
  54. }
  55. function displayError(error){
  56. console.log(error.statusText);
  57. }
  58. }
Add Comment
Please, Sign In to add comment