Advertisement
Guest User

Untitled

a guest
Nov 25th, 2016
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function attachEvents() {
  2.     const id = "kid_rkFRw1QGl";
  3.     const user = "guest";
  4.     const pass = "guest";
  5.     const b64 = btoa(`${user}:${pass}`);
  6.     const headers = {Authorization: `Basic ${b64}`};
  7.     const url = 'https://baas.kinvey.com/apdata/${id}/biggestCatches';
  8.  
  9.     $('.add').click(function () {
  10.         let el = '#addForm';
  11.         let angler = $(el).find(`.angler`).val();
  12.         let weight = $(el).find(`.weight`).val();
  13.         let species = $(el).find(`.species`).val();
  14.         let location = $(el).find(`.location`).val();
  15.         let bait = $(el).find(`.bait`).val();
  16.         let captureTime = $(el).find(`.captureTime`).val();
  17.         let data = JSON.stringify({
  18.             "angler": angler,
  19.             "weight": weight,
  20.             "species": species,
  21.             "location": location,
  22.             "bait": bait,
  23.             "captureTime": captureTime
  24.         });
  25.         $.ajax({
  26.             method: 'POST',
  27.             url: url,
  28.             data: data,
  29.             headers: headers,
  30.             contentType: 'application/json'
  31.         })
  32.             .then(load)
  33.             .catch(err);
  34.         function clear() {
  35.             $(el).find(`.angler`).val('');
  36.             $(el).find(`.weight`).val('');
  37.             $(el).find(`.species`).val('');
  38.             $(el).find(`.location`).val('');
  39.             $(el).find(`.bait`).val('');
  40.             $(el).find(`.captureTime`).val('');
  41.         }
  42.  
  43.     });
  44.     $('.load').click(load);
  45.  
  46.     function load() {
  47.         $('#catches').empty();
  48.         $.ajax({
  49.             method: 'GET',
  50.             url: url,
  51.             contentType: 'application/json'
  52.         })
  53.             .then(ape)
  54.             .catch(err);
  55.  
  56.     }
  57.     function ape(dt) {
  58.         for (let v of dt) {
  59.             let HTML = getCatchHtml(v);
  60.             HTML.find('.delete').click(del);
  61.             HTML.find('.update').click(up);
  62.  
  63.             $('#catches').append(HTML);
  64.  
  65.         }
  66.  
  67.     }
  68.  
  69.     function del() {
  70.         let p = $(this).parent();
  71.         let i = parent.attr('data-id');
  72.  
  73.         $.ajax({
  74.             method: "DELETE",
  75.             url: url + i,
  76.             headers: headers,
  77.             contentType: 'application/json'
  78.         })
  79.             .then();
  80.         p.remove();
  81.  
  82.     }
  83.  
  84.     function up() {
  85.         let p = $(this).parent();
  86.         let i = p.attr('data-id');
  87.  
  88.         let cap = createNewCapture(p);
  89.  
  90.  
  91.         $.ajax({
  92.             method: "PUT",
  93.             url: url + i,
  94.             headers: headers,
  95.             data: cap,
  96.             contentType: 'application/json'
  97.         })
  98.             .then()
  99.  
  100.     }
  101.  
  102.     function err() {
  103.         console.log('ERROR');
  104.     }
  105.  
  106. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement