Advertisement
Guest User

Untitled

a guest
Jul 23rd, 2018
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.87 KB | None | 0 0
  1. function attachEvents() {
  2. const URL = 'https://baas.kinvey.com/appdata/kid_Sy6UZBL5z/biggestCatches';
  3. const USERNAME = 'test';
  4. const PASSWORD = 'test';
  5. const base64auth = btoa(USERNAME + ':' + PASSWORD);
  6. const authHeaders = {'Authorization': 'Basic ' + base64auth};
  7.  
  8. $('.add').on('click', addCatchAjax);
  9. $('.load').on('click', loadAllCatchAjax);
  10.  
  11. function addCatchAjax() {
  12. let select = $('#aside').find('fieldset input');
  13.  
  14. let angler = select[0].value;
  15. let weight = parseFloat(select[1].value);
  16. let species = select[2].value;
  17. let location = select[3].value;
  18. let bait = select[4].value;
  19. let captureTime = Number(select[5].value);
  20.  
  21. let inputBody = JSON.stringify({
  22. 'angler': angler,
  23. 'weight': weight,
  24. 'species': species,
  25. 'location': location,
  26. 'bait': bait,
  27. 'captureTime': captureTime,
  28. });
  29.  
  30. $.ajax({
  31. method: 'POST',
  32. url: URL,
  33. data: inputBody,
  34. headers: authHeaders,
  35. }).then()
  36. .catch(errorFunc);
  37. }
  38.  
  39. function loadAllCatchAjax() {
  40. $.ajax({
  41. method: 'GET',
  42. url: URL,
  43. headers: authHeaders,
  44. }).then(loadAll)
  45. .catch(errorFunc);
  46.  
  47. function loadAll(res) {
  48. let catches = $('#catches');
  49. catches.empty();
  50. for (let obj of res) {
  51. catches.append($(`<div class="catch" data-id="${obj._id}">`)
  52. .append($('<label>').text('Angler'))
  53. .append($(`<input type="text" class="angler" value="${obj.angler}"/>`))
  54. .append($('<label>').text('Weight'))
  55. .append($(`<input type="number" class="weight" value="${obj.weight}"/>`))
  56. .append($('<label>').text('Species'))
  57. .append($(`<input type="text" class="species" value="${obj.species}"/>`))
  58. .append($('<label>').text('Location'))
  59. .append($(`<input type="text" class="location" value="${obj.location}"/>`))
  60. .append($('<label>').text('Bait'))
  61. .append($(`<input type="text" class="bait" value="${obj.bait}"/>`))
  62. .append($('<label>').text('Capture Time'))
  63. .append($(`<input type="number" class="captureTime" value="${obj.captureTime}"/>`))
  64. .append($('<button class="update">Update</button>').on('click', updateCatch))
  65. .append($('<button class="delete">Delete</button>').on('click', deleteCatch)))
  66. }
  67. }
  68. }
  69.  
  70. function deleteCatch() {
  71. let id = $(this).parent();
  72. $.ajax({
  73. method: 'DELETE',
  74. url: URL + '/' + id.attr('data-id'),
  75. headers: authHeaders
  76. }).then(id.remove())
  77. .catch(errorFunc);
  78. }
  79.  
  80. function updateCatch() {
  81. let id = $(this).parent();
  82.  
  83. let select = $(id.find('input'));
  84.  
  85. let angler = select[0].value;
  86. let weight = parseFloat(select[1].value);
  87. let species = select[2].value;
  88. let location = select[3].value;
  89. let bait = select[4].value;
  90. let captureTime = Number(select[5].value);
  91.  
  92. let inputData = JSON.stringify({
  93. 'angler': angler,
  94. 'weight': weight,
  95. 'species': species,
  96. 'location': location,
  97. 'bait': bait,
  98. 'captureTime': captureTime,
  99. });
  100.  
  101. $.ajax({
  102. method: 'PUT',
  103. url: URL + '/' + id.attr('data-id'),
  104. data: inputData,
  105. headers: authHeaders
  106. }).then()
  107. .catch(errorFunc);
  108. }
  109.  
  110. function errorFunc(err) {
  111. console.log(err);
  112. }
  113. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement