Advertisement
MiroslavKisov

Fisher_Game

Mar 28th, 2019
203
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.         aside : '#aside',
  12.         update : '.update',
  13.         load : '.load',
  14.         delete : '.delete',
  15.         add : '.add',
  16.     }
  17.  
  18.     $(selectors.load).on('click', loadCatches);
  19.     $(selectors.add).on('click', addCatch);
  20.  
  21.     //CATCHES INPUT FIELDS
  22.  
  23.     let catchDiv = $(selectors.catches);
  24.  
  25.     let anglerEdit = catchDiv.find($(selectors.angler));
  26.     let weightEdit = catchDiv.find($(selectors.weight));
  27.     let speciesEdit = catchDiv.find($(selectors.species));
  28.     let locationEdit = catchDiv.find($(selectors.location));
  29.     let baitEdit = catchDiv.find($(selectors.bait));
  30.     let captureTimeEdit = catchDiv.find($(selectors.captureTime));
  31.  
  32.     //CREATE CATCH INPUT FIELDS
  33.  
  34.     let asideDiv = $(selectors.aside);
  35.  
  36.     let anglerCreate = asideDiv.find($(selectors.angler));
  37.     let weightCreate = asideDiv.find($(selectors.weight));
  38.     let speciesCreate = asideDiv.find($(selectors.species));
  39.     let locationCreate = asideDiv.find($(selectors.location));
  40.     let baitCreate = asideDiv.find($(selectors.bait));
  41.     let captureTimeCreate = asideDiv.find($(selectors.captureTime));
  42.  
  43.     const appID = 'kid_HyMYN45dV';
  44.     const username = 'guest';
  45.     const password = 'guest';
  46.     const base64auth = btoa(username + ":" + password);
  47.     const authHeaders = {'Authorization': 'Basic ' + base64auth, 'Content-Type': 'application/json'};
  48.     const baseUrl ='https://baas.kinvey.com/appdata/'+ appID + '/biggestCatches';
  49.  
  50.     function loadCatches() {
  51.  
  52.         $.ajax({
  53.             method : 'GET',
  54.             url : baseUrl,
  55.             headers : authHeaders,
  56.         })
  57.         .then(displayCatches)
  58.         .catch(displayError);
  59.     }
  60.  
  61.     function addCatch() {
  62.  
  63.     }
  64.  
  65.     function updateCatch(catchId) {
  66.  
  67.         let testObj = {
  68.             angler: "Gosho",
  69.             weight: 500,
  70.             species: "Fish",
  71.             location: "Varna",
  72.             bait: "Worm",
  73.             captureTime: 500,
  74.         };
  75.  
  76.         $.ajax({
  77.             method : 'PUT',
  78.             url : baseUrl + '/' + catchId,
  79.             data: JSON.stringify(testObj),
  80.             headers : authHeaders,
  81.         })
  82.         .then(loadCatches)
  83.         .catch(displayError);
  84.     }
  85.  
  86.     function deleteCatch(catchId) {
  87.        
  88.     }
  89.  
  90.     function displayCatches(catches) {
  91.  
  92.         for(const c of catches) {
  93.             catchDiv
  94.             .append($('<div>').attr('data-id', c._id)
  95.             .append(`<label>Angler</label>` +
  96.             `<input type="text" class="angler" value="${c.angler}"/>`+
  97.             `<label>Weight</label>`+
  98.             `<input type="number" class="weight" value="${c.weight}"/>`+
  99.             `<label>Species</label>`+
  100.             `<input type="text" class="species" value="${c.species}"/>`+
  101.             `<label>Location</label>`+
  102.             `<input type="text" class="location" value="${c.location}"/>`+
  103.             `<label>Bait</label>`+
  104.             `<input type="text" class="bait" value="${c.bait}"/>`+
  105.             `<label>Capture Time</label>`+
  106.             `<input type="number" class="captureTime" value="${c.captureTime}"/>`)
  107.             .append($('<button>').addClass('update').text('Update').on('click', updateCatch.bind(this, c._id)))
  108.             .append($('<button>').addClass('delete').text('Delete').on('click', deleteCatch.bind(this, c._id))));
  109.         }
  110.     }
  111.  
  112.     function displayError(error) {
  113.         $('body').html(error.statusText);
  114.     }
  115. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement