Guest User

Untitled

a guest
Nov 19th, 2016
287
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function attachEvents() {
  2. const kinveyAppId = "kid_H1_9pM0Wx";
  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.  
  12. $(".add").click(proceedData);
  13. $(".load").click(loadCatches);
  14.  
  15. function proceedData(){
  16. let angler = $("#addForm .angler").val();
  17. let weight = $("#addForm .weight").val();
  18. let species = $("#addForm .species").val();
  19. let location = $("#addForm .location").val();
  20. let bait = $("#addForm .bait").val();
  21. let captureTime = $("#addForm .captureTime").val();
  22.  
  23. let obj = {
  24. angler: angler,
  25. weight: weight,
  26. species: species,
  27. location: location,
  28. bait: bait,
  29. captureTime: captureTime
  30. };
  31.  
  32. $.ajax({
  33. method: "POST",
  34. url: `https://baas.kinvey.com/appdata/${kinveyAppId}/biggestCatches`,
  35. data: JSON.stringify(obj),
  36. headers: authHeaders
  37. })
  38. .then(loadCatches)
  39. .catch(displayError);
  40.  
  41.  
  42. }
  43. function loadCatches(){
  44. let url = "https://baas.kinvey.com/appdata/kid_H1_9pM0Wx/biggestCatches";
  45. let request = {
  46. method: "GET",
  47. url: url,
  48. headers: authHeaders
  49. };
  50. $.post(request)
  51. .then(displayCatches)
  52. .catch(displayError);
  53. }
  54. function displayCatches(data){
  55. $("#catches").children().remove();
  56. for(let caughtFish of data){
  57. let parentDiv = $("<div class='catch'>").attr('data-id', caughtFish._id);
  58. $("<label>Angler</label>").appendTo($(parentDiv));
  59. $('<input type="text" class="angler"/>').val(caughtFish.angler).appendTo($(parentDiv));
  60. $("<label>Weight</label>").appendTo($(parentDiv));
  61. $('<input type="text" class="weight"/>').val(Number(caughtFish.weight)).appendTo($(parentDiv));
  62. $("<label>Species</label>").appendTo($(parentDiv));
  63. $('<input type="text" class="species"/>').val(caughtFish.species).appendTo($(parentDiv));
  64. $("<label>Location</label>").appendTo($(parentDiv));
  65. $('<input type="text" class="location"/>').val(caughtFish.location).appendTo($(parentDiv));
  66. $("<label>Bait</label>").appendTo($(parentDiv));
  67. $('<input type="text" class="bait"/>').val(caughtFish.bait).appendTo($(parentDiv));
  68. $("<label>Capture Time</label>").appendTo($(parentDiv));
  69. $('<input type="text" class="captureTime"/>').val(Number(caughtFish.captureTime)).appendTo($(parentDiv));
  70. $('<button class="update">Update</button>').click(function(){
  71. updateCatch(caughtFish._id);
  72. }).appendTo($(parentDiv));
  73. $('<button class="delete">Delete</button>').click(function(){
  74. deleteCatch(caughtFish._id);
  75. }).appendTo($(parentDiv));
  76. $(parentDiv).appendTo($("#catches"));
  77. }
  78.  
  79. }
  80. function updateCatch(catchId){
  81. let angler = $( `.catch[data-id='${catchId}'] .angler` ).val();
  82. let weight = $( `.catch[data-id='${catchId}'] .weight` ).val();
  83. let species = $( `.catch[data-id='${catchId}'] .species` ).val();
  84. let location = $( `.catch[data-id='${catchId}'] .location` ).val();
  85. let bait = $( `.catch[data-id='${catchId}'] .bait` ).val();
  86. let captureTime = $( `.catch[data-id='${catchId}'] .captureTime` ).val();
  87.  
  88. let obj = {
  89. angler: angler,
  90. weight: weight,
  91. species: species,
  92. location: location,
  93. bait: bait,
  94. captureTime: captureTime
  95. };
  96. let request = {
  97. method: "PUT",
  98. headers: authHeaders,
  99. data: JSON.stringify(obj),
  100. url: 'https://baas.kinvey.com/appdata/kid_H1_9pM0Wx/biggestCatches/' + catchId
  101. };
  102. $.ajax(request)
  103. .then(function () {
  104. console.log('fish updated')
  105. })
  106. .catch(displayError)
  107. }
  108. function deleteCatch(catchId){
  109. $(`div[data-id='${catchId}']`).remove();
  110. let request = {
  111. method: "DELETE",
  112. headers: authHeaders,
  113. url: 'https://baas.kinvey.com/appdata/kid_H1_9pM0Wx/biggestCatches/' + catchId
  114. };
  115. $.ajax(request)
  116. .then(function () {
  117. console.log('fish deleted')
  118. })
  119. .catch(displayError);
  120.  
  121. }
  122. function displayError(error){
  123. console.log(error.statusText);
  124. }
  125.  
  126. }
RAW Paste Data