Guest User

Untitled

a guest
Nov 26th, 2016
42
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.15 KB | None | 0 0
  1. function attachEvents (){
  2.  
  3. const kinveyAppId = "kid_Hkg9apVGl";
  4. const serviceUrl = "https://baas.kinvey.com/appdata/" + kinveyAppId + '/catches';
  5. const kinveyUsername = "test";
  6. const kinveyPassword = "test";
  7. const base64auth = btoa(kinveyUsername + ":" + kinveyPassword);
  8. const authHeaders = {
  9. "Authorization": "Basic " + base64auth,
  10. "Content-Type": "application/json"
  11. };
  12.  
  13.  
  14.  
  15. $('.load').click(getAllCatches);
  16. $('.add').click(addCatch);
  17.  
  18.  
  19. function getAllCatches (){
  20.  
  21. $.get({
  22.  
  23. url: serviceUrl,
  24. headers: authHeaders
  25.  
  26. }).then(displayCatches);
  27.  
  28. }
  29.  
  30. function displayCatches(catches){
  31.  
  32. //$('#catches').empty();
  33.  
  34. for(let cat of catches) {
  35.  
  36. $('#catches').append(`<div class="catch" data-id="<${cat._id}>">
  37. <label>Angler</label>
  38. <input type="text" class="angler" value="${cat.angler}"/>
  39. <label>Weight</label>
  40. <input type="number" class="weight" value="${Number(cat.weight)}"/>
  41. <label>Species</label>
  42. <input type="text" class="species" value="${cat.species}"/>
  43. <label>Location</label>
  44. <input type="text" class="location" value="${cat.location}"/>
  45. <label>Bait</label>
  46. <input type="text" class="bait" value="${cat.bait}"/>
  47. <label>Capture Time</label>
  48. <input type="number" class="captureTime" value="${Number(cat.captureTime)}"/>
  49. <button class="update">Update</button>
  50. <button class="delete">Delete</button>
  51. </div>`);
  52.  
  53.  
  54. }
  55.  
  56. $('.delete').click(function (){
  57. deleteCatch($(this).parent());
  58. $(this).parent().remove();
  59.  
  60. });
  61. $('.update').click(function (){
  62. updateCatch($(this).parent());
  63.  
  64. });
  65.  
  66. //let entries = $('#catches').find(".catch");
  67. //console.log(entries.length)
  68.  
  69. }
  70.  
  71. function addCatch (){
  72.  
  73. let cattch = {};
  74.  
  75. $('#addForm :input').each(function(){
  76.  
  77. cattch[this.className] = $(this).val();
  78. });
  79.  
  80. delete cattch.add;
  81.  
  82. //console.log(cattch);
  83. $.post({
  84.  
  85. url: serviceUrl,
  86. headers: authHeaders,
  87. data: cattch
  88.  
  89. })
  90.  
  91.  
  92. }
  93.  
  94. function deleteCatch (parent){
  95.  
  96. let id = parent[0].attributes['data-id'].value.replace(/[<>]/g, '');
  97.  
  98. $.ajax({
  99. type: 'DELETE',
  100. url: serviceUrl + '/' + id,
  101. headers: authHeaders
  102.  
  103. })
  104. }
  105.  
  106.  
  107. function updateCatch (parent) {
  108. let id = parent[0].attributes['data-id'].value.replace(/[<>]/g, '');
  109.  
  110. let updateCatch = {};
  111.  
  112. $(parent.children().find('input')).each(function(){
  113.  
  114. updateCatch[this.className] = $(this).val();
  115. });
  116.  
  117. //console.log(updateCatch)
  118.  
  119.  
  120. $.ajax({
  121. type: 'PUT',
  122. url: serviceUrl + '/' + id,
  123. headers: authHeaders,
  124. data: ''
  125.  
  126. })
  127.  
  128.  
  129. }
  130.  
  131.  
  132.  
  133. }
Add Comment
Please, Sign In to add comment