Advertisement
Stan08

WrongCode

Apr 4th, 2019
638
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.42 KB | None | 0 0
  1.  
  2. function attachEvents(){
  3.  
  4. const bUrl = 'https://baas.kinvey.com/';
  5. const appKey = 'kid_ryfZ8eGYE';
  6. const endPoint = 'biggestCatches';
  7. const userName = 'StanZash1';
  8. const password = '123456';
  9. const headers = {
  10. 'Authorization': `Basic ${btoa(userName + ':' + password)}`,
  11. 'Content-Type': 'application/json'
  12. };
  13.  
  14. $('.load').on('click', loadCatches);
  15.  
  16.  
  17. $('.add').on('click', addCatch);
  18.  
  19. async function loadCatches(){
  20.  
  21. $('#catches').empty();
  22. let catches = await $.ajax({
  23. url: bUrl + 'appdata/' + appKey + '/' + endPoint,
  24. method: 'GET',
  25. headers
  26. });
  27. for (let el of catches){
  28. let div = $(`
  29. <div class="catch" data-id="${el._id}">
  30. <label>Angler</label>
  31. <input type="text" class="angler" value="${el.angler}"/>
  32. <label>Weight</label>
  33. <input type="number" class="weight" value="${el.weight}"/>
  34. <label>Species</label>
  35. <input type="text" class="species" value="${el.species}"/>
  36. <label>Location</label>
  37. <input type="text" class="location" value="${el.location}"/>
  38. <label>Bait</label>
  39. <input type="text" class="bait" value="${el.bait}"/>
  40. <label>Capture Time</label>
  41. <input type="number" class="captureTime" value="${el.captureTime}"/>
  42. </div>
  43. `)
  44. let updateBtn = $('<button class="update">Update</button>');
  45.  
  46.  
  47. let deleteBtn = $('<button class="delete">Delete</button>');
  48.  
  49. updateBtn.on('click', console.log('d'));
  50. deleteBtn.on('click', console.log('s'));
  51.  
  52. div.append(updateBtn)
  53. div.append(deleteBtn)
  54.  
  55. $('#catches').append(div);
  56.  
  57.  
  58. }
  59.  
  60. }
  61.  
  62. async function addCatch(){
  63.  
  64. let angler = $('#addForm input.angler').val();
  65. let weight = +$('#addForm input.weight').val();
  66. let species = $('#addForm input.species').val();
  67. let location = $('#addForm input.location').val();
  68. let bait = $('#addForm input.bait').val();
  69. let captureTime = +$('#addForm input.captureTime').val();
  70.  
  71. let catchObj = {
  72. angler,
  73. weight,
  74. species,
  75. location,
  76. bait,
  77. captureTime
  78. };
  79.  
  80. await $.ajax({
  81. url: bUrl + 'appdata/' + appKey + '/' + endPoint,
  82. method: 'POST',
  83. headers,
  84. data: JSON.stringify(catchObj)
  85. })
  86. loadCatches();
  87. }
  88.  
  89.  
  90. async function updateCatch(){
  91.  
  92. console.log('d');
  93.  
  94. // console.log($(this));
  95.  
  96. // let angler = $(this).parent().find('input.angler').val();
  97. // let weight = +$(this).parent().find('input.weight').val();
  98. // let species = $(this).parent().find('input.species').val();
  99. // let location = $(this).parent().find('input.location').val();
  100. // let bait = $(this).parent().find('input.bait').val();
  101. // let captureTime = +$(this).parent().find('input.captureTime').val();
  102.  
  103. // let newCatch = {
  104. // angler,
  105. // weight,
  106. // species,
  107. // location,
  108. // bait,
  109. // captureTime
  110. // };
  111.  
  112. // let id = $(this).parent().data('id');
  113. // try{
  114.  
  115.  
  116.  
  117. // await $.ajax({
  118. // url: bUrl + 'appdata/' + appKey + '/'+endPoint+'/'+id,
  119. // method: 'PUT',
  120. // data: JSON.stringify(newCatch),
  121. // headers
  122.  
  123. // });
  124.  
  125. // }catch(err){
  126. // console.log(err);
  127. // }
  128. // loadCatches()
  129. }
  130. function deleteCatch(){
  131.  
  132. }
  133.  
  134. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement