Advertisement
Guest User

Fisher-Game

a guest
Nov 25th, 2016
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.62 KB | None | 0 0
  1. function attachEvents() {
  2. $('.add').click(addNewCatch);
  3. $('.load').click(getAllCatches);
  4.  
  5. const kinveyAppId = "kid_ByWSzfUfg";
  6. const serverUrl = "https://baas.kinvey.com/appdata/" + kinveyAppId + "/biggestCatches";
  7. const kinveyUsername = "guest";
  8. const kinveyPassword = "guest";
  9. const base64auth = btoa(kinveyUsername +":"+ kinveyPassword);
  10. const authHeaders = { "Authorization": "Basic " + base64auth };
  11.  
  12. function getAllCatches() {
  13. let loadCatches = {
  14. url: serverUrl,
  15. headers: authHeaders
  16. };
  17.  
  18. $.ajax(loadCatches)
  19. .then(displayCatches)
  20. .catch(displayError)
  21. }
  22.  
  23. function displayCatches(catchesArr) {
  24. $('#catches').empty();
  25.  
  26. for (let fishCatch of catchesArr) {
  27. let divCatch = $('<div class="catch">').attr("data-id", fishCatch._id).append(
  28. $('<label>Angler</label>'),
  29. $('<input type="text" class="angler"/>').attr("value", fishCatch.angler),
  30. $('<label>Weight</label>'),
  31. $('<input type="number" class="weight"/>').attr("value", fishCatch.weight),
  32. $('<label>Species</label>'),
  33. $('<input type="text" class="species"/>').attr("value", fishCatch.species),
  34. $('<label>Location</label>'),
  35. $('<input type="text" class="location"/>').attr("value", fishCatch.location),
  36. $('<label>Bait</label>'),
  37. $('<input type="text" class="bait"/>').attr("value", fishCatch.bait),
  38. $('<label>Capture Time</label>'),
  39. $('<input type="number" class="captureTime"/>').attr("value", fishCatch.captureTime),
  40. $('<button class="update">Update</button>').click(() => updateCatch(fishCatch._id)),
  41. $('<button class="delete">Delete</button>').click(() => deleteCatch(fishCatch._id))
  42. );
  43. $('#catches').append(divCatch)
  44. }
  45. }
  46.  
  47. function updateCatch(id) {
  48. let fishFormData = $("div[data-id='" + id + "']").contents().filter( "input" );
  49.  
  50. let catchData = {
  51. angler: $(fishFormData[0]).val(),
  52. weight: $(fishFormData[1]).val(),
  53. species: $(fishFormData[2]).val(),
  54. location: $(fishFormData[3]).val(),
  55. bait: $(fishFormData[4]).val(),
  56. captureTime: $(fishFormData[5]).val()
  57. };
  58.  
  59. let updateReq = {
  60. method: "PUT",
  61. url: serverUrl + "/" + id,
  62. headers: authHeaders,
  63. dataType: "application/json",
  64. data: catchData
  65. };
  66.  
  67. $.ajax(updateReq)
  68. .then(getAllCatches)
  69. .catch(displayError)
  70. }
  71.  
  72. function deleteCatch(id) {
  73. let requestDel = {
  74. method: "DELETE",
  75. url: serverUrl + `/${id}`,
  76. headers: authHeaders
  77. };
  78.  
  79. $.ajax(requestDel)
  80. .then(getAllCatches)
  81. .catch(displayError)
  82. }
  83.  
  84. function addNewCatch() {
  85. let createNewCatch = $("#addForm").contents().filter( "input" );
  86.  
  87. let catchData = {
  88. angler: $(createNewCatch[0]).val(),
  89. weight: $(createNewCatch[1]).val(),
  90. species: $(createNewCatch[2]).val(),
  91. location: $(createNewCatch[3]).val(),
  92. bait: $(createNewCatch[4]).val(),
  93. captureTime: $(createNewCatch[5]).val()
  94. };
  95.  
  96. for (let elem of createNewCatch) {
  97. $(elem).val("")
  98. }
  99.  
  100. let createReq = {
  101. method: "POST",
  102. url: serverUrl,
  103. headers: authHeaders,
  104. dataType: "application/json",
  105. data: catchData
  106. };
  107.  
  108. $.ajax(createReq)
  109. .then(getAllCatches)
  110. .catch(displayError)
  111. }
  112.  
  113. function displayError(err) {
  114. alert(`${err.status}: ${err.statusText}`)
  115. }
  116. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement