Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function attachEvents() {
- const kinveyAppId = "kid_H1_9pM0Wx";
- const serviceUrl = "https://baas.kinvey.com/appdata/" + kinveyAppId;
- const kinveyUsername = "guest";
- const kinveyPassword = "guest";
- const base64auth = btoa(kinveyUsername + ":" + kinveyPassword);
- const authHeaders = {
- "Authorization": "Basic " + base64auth,
- "Content-type": "application/json"
- };
- $(".add").click(proceedData);
- $(".load").click(loadCatches);
- function proceedData(){
- let angler = $("#addForm .angler").val();
- let weight = $("#addForm .weight").val();
- let species = $("#addForm .species").val();
- let location = $("#addForm .location").val();
- let bait = $("#addForm .bait").val();
- let captureTime = $("#addForm .captureTime").val();
- let obj = {
- angler: angler,
- weight: weight,
- species: species,
- location: location,
- bait: bait,
- captureTime: captureTime
- };
- $.ajax({
- method: "POST",
- url: `https://baas.kinvey.com/appdata/${kinveyAppId}/biggestCatches`,
- data: JSON.stringify(obj),
- headers: authHeaders
- })
- .then(loadCatches)
- .catch(displayError);
- }
- function loadCatches(){
- let url = "https://baas.kinvey.com/appdata/kid_H1_9pM0Wx/biggestCatches";
- let request = {
- method: "GET",
- url: url,
- headers: authHeaders
- };
- $.post(request)
- .then(displayCatches)
- .catch(displayError);
- }
- function displayCatches(data){
- $("#catches").children().remove();
- for(let caughtFish of data){
- let parentDiv = $("<div class='catch'>").attr('data-id', caughtFish._id);
- $("<label>Angler</label>").appendTo($(parentDiv));
- $('<input type="text" class="angler"/>').val(caughtFish.angler).appendTo($(parentDiv));
- $("<label>Weight</label>").appendTo($(parentDiv));
- $('<input type="text" class="weight"/>').val(Number(caughtFish.weight)).appendTo($(parentDiv));
- $("<label>Species</label>").appendTo($(parentDiv));
- $('<input type="text" class="species"/>').val(caughtFish.species).appendTo($(parentDiv));
- $("<label>Location</label>").appendTo($(parentDiv));
- $('<input type="text" class="location"/>').val(caughtFish.location).appendTo($(parentDiv));
- $("<label>Bait</label>").appendTo($(parentDiv));
- $('<input type="text" class="bait"/>').val(caughtFish.bait).appendTo($(parentDiv));
- $("<label>Capture Time</label>").appendTo($(parentDiv));
- $('<input type="text" class="captureTime"/>').val(Number(caughtFish.captureTime)).appendTo($(parentDiv));
- $('<button class="update">Update</button>').click(function(){
- updateCatch(caughtFish._id);
- }).appendTo($(parentDiv));
- $('<button class="delete">Delete</button>').click(function(){
- deleteCatch(caughtFish._id);
- }).appendTo($(parentDiv));
- $(parentDiv).appendTo($("#catches"));
- }
- }
- function updateCatch(catchId){
- let angler = $( `.catch[data-id='${catchId}'] .angler` ).val();
- let weight = $( `.catch[data-id='${catchId}'] .weight` ).val();
- let species = $( `.catch[data-id='${catchId}'] .species` ).val();
- let location = $( `.catch[data-id='${catchId}'] .location` ).val();
- let bait = $( `.catch[data-id='${catchId}'] .bait` ).val();
- let captureTime = $( `.catch[data-id='${catchId}'] .captureTime` ).val();
- let obj = {
- angler: angler,
- weight: weight,
- species: species,
- location: location,
- bait: bait,
- captureTime: captureTime
- };
- let request = {
- method: "PUT",
- headers: authHeaders,
- data: JSON.stringify(obj),
- url: 'https://baas.kinvey.com/appdata/kid_H1_9pM0Wx/biggestCatches/' + catchId
- };
- $.ajax(request)
- .then(function () {
- console.log('fish updated')
- })
- .catch(displayError)
- }
- function deleteCatch(catchId){
- $(`div[data-id='${catchId}']`).remove();
- let request = {
- method: "DELETE",
- headers: authHeaders,
- url: 'https://baas.kinvey.com/appdata/kid_H1_9pM0Wx/biggestCatches/' + catchId
- };
- $.ajax(request)
- .then(function () {
- console.log('fish deleted')
- })
- .catch(displayError);
- }
- function displayError(error){
- console.log(error.statusText);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement