Advertisement
Guest User

Untitled

a guest
Mar 31st, 2019
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function attachEvents() {
  2.  
  3.     let selectors = {
  4.         angler : '.angler',
  5.         weight : '.weight',
  6.         species : '.species',
  7.         location : '.location',
  8.         bait : '.bait',
  9.         captureTime : '.captureTime',
  10.         catches : '#catches',
  11.         catch : '.catch',
  12.         aside : '#aside',
  13.         update : '.update',
  14.         load : '.load',
  15.         delete : '.delete',
  16.         add : '.add',
  17.     }
  18.  
  19.     $(selectors.load).on('click', loadCatches);
  20.     $(selectors.add).on('click', addCatch);
  21.  
  22.     let catchDiv = $(selectors.catches);
  23.  
  24.     let asideDiv = $(selectors.aside);
  25.  
  26.     const appID = 'kid_HyMYN45dV';
  27.     const username = 'guest';
  28.     const password = 'guest';
  29.     const base64auth = btoa(username + ":" + password);
  30.     const authHeaders = {'Authorization': 'Basic ' + base64auth, 'Content-Type': 'application/json'};
  31.     const baseUrl ='https://baas.kinvey.com/appdata/'+ appID + '/biggestCatches';
  32.  
  33.     function loadCatches() {
  34.  
  35.         catchDiv.empty();
  36.  
  37.         $.ajax({
  38.             method : 'GET',
  39.             url : baseUrl,
  40.             headers : authHeaders,
  41.         })
  42.         .then(displayCatches)
  43.         .catch(displayError);
  44.  
  45.         function displayCatches(catches) {
  46.  
  47.             for(const c of catches) {
  48.                 catchDiv
  49.                 .append(`<div class="catch" data-id="${c._id}">
  50.                 <label>Angler</label>
  51.                 <input type="text" class="angler" value="${c.angler}"/>
  52.                 <label>Weight</label>
  53.                 <input type="number" class="weight" value="${c.weight}"/>
  54.                 <label>Species</label>
  55.                 <input type="text" class="species" value="${c.species}"/>
  56.                 <label>Location</label>
  57.                 <input type="text" class="location" value="${c.location}"/>
  58.                 <label>Bait</label>
  59.                 <input type="text" class="bait" value="${c.bait}"/>
  60.                 <label>Capture Time</label>
  61.                 <input type="number" class="captureTime" value="${c.captureTime}"/>
  62.                 <button class="update">Update</button>
  63.                 <button class="delete">Delete</button>
  64.                 </div>`);
  65.  
  66.                 $(selectors.catch).children(selectors.update).on('click', updateCatch.bind(this, c));
  67.                 $(selectors.catch).children(selectors.delete).on('click', deleteCatch.bind(this, c._id));
  68.             }
  69.         }
  70.     }
  71.  
  72.     function addCatch() {
  73.  
  74.         let anglerCreate = asideDiv.find($(selectors.angler)).val();
  75.         let weightCreate = Number(asideDiv.find($(selectors.weight)).val());
  76.         let speciesCreate = asideDiv.find($(selectors.species)).val();
  77.         let locationCreate = asideDiv.find($(selectors.location)).val();
  78.         let baitCreate = asideDiv.find($(selectors.bait)).val();
  79.         let captureTimeCreate = Number(asideDiv.find($(selectors.captureTime)).val());
  80.  
  81.         let newCatch = {
  82.             angler:anglerCreate,
  83.             weight:weightCreate,
  84.             species:speciesCreate,
  85.             location:locationCreate,
  86.             bait:baitCreate,
  87.             captureTime:captureTimeCreate,
  88.         };
  89.  
  90.         $.ajax({
  91.             method : 'POST',
  92.             url : baseUrl,
  93.             data: JSON.stringify(newCatch),
  94.             headers : authHeaders,
  95.         })
  96.         .then(loadCatches)
  97.         .catch(displayError);
  98.     }
  99.  
  100.     function updateCatch(c) {
  101.  
  102.         let parentDiv = catchDiv;
  103.         let anglerEdit = parentDiv.find($(selectors.angler)).val();
  104.         let weightEdit = Number(parentDiv.find($(selectors.weight)).val());
  105.         let speciesEdit= parentDiv.find($(selectors.species)).val();
  106.         let locationEdit = parentDiv.find($(selectors.location)).val();
  107.         let baitEdit = parentDiv.find($(selectors.bait)).val();
  108.         let captureTimeEdit = Number(parentDiv.find($(selectors.captureTime)).val());
  109.        
  110.         let currentCatch = {
  111.             angler:anglerEdit,
  112.             weight:weightEdit,
  113.             species:speciesEdit,
  114.             location:locationEdit,
  115.             bait:baitEdit,
  116.             captureTime:captureTimeEdit,
  117.         };
  118.  
  119.         $.ajax({
  120.             method : 'PUT',
  121.             url : baseUrl + '/' + c._id,
  122.             data: JSON.stringify(currentCatch),
  123.             headers : authHeaders,
  124.         })
  125.         .then(loadCatches)
  126.         .catch(displayError);
  127.     }
  128.  
  129.     function deleteCatch(catchId) {
  130.         $.ajax({
  131.             method : 'DELETE',
  132.             url : baseUrl + '/' + catchId,
  133.             headers : authHeaders,
  134.         })
  135.         .then(loadCatches)
  136.         .catch(displayError);
  137.     }
  138.  
  139.     function displayError(error) {
  140.         console.log(error.statusText);
  141.     }
  142. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement