Advertisement
Guest User

Untitled

a guest
Apr 12th, 2015
293
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. initializeCountries();
  2. initializeTowns();
  3. var currentTownIndex = 0;
  4. var headers = {
  5.     'X-Parse-Application-Id': 'cDOllTGD0mgH2eJVkoAEQX8Kl5uG9V5BOF99Rxz0',
  6.     'X-Parse-REST-API-Key': 'Chn6ZbJZKOcaI42fa2wQdrgoMZSBg88eWJmeOmVd'
  7. };
  8.  
  9. function initializeCountries() {
  10.     $.ajax({
  11.         method: 'GET',
  12.         headers: headers,
  13.         url: 'https://api.parse.com/1/classes/Country'
  14.     }).success(function (data) {
  15.         var countriesContainer = $('#countriesContainer');
  16.         var countries = $('<ul></ul>');
  17.  
  18.         for (var country in data.results) {
  19.             var liElement = $('<li></li>').text(data.results[country].name);
  20.  
  21.             var deleteButton = document.createElement('button');
  22.             deleteButton.innerText = 'Delete';
  23.             deleteButton.setAttribute('onclick',
  24.                 'javascript: deleteCountry("' + data.results[country].objectId + '")');
  25.  
  26.             var editButton = document.createElement('button');
  27.             editButton.innerText = 'Edit';
  28.             editButton.setAttribute('onclick',
  29.                 'javascript: editCountry("' + data.results[country].objectId + '")');
  30.  
  31.             liElement.append(deleteButton);
  32.             liElement.append(editButton);
  33.  
  34.             countries.append(liElement);
  35.         }
  36.  
  37.         countriesContainer.append(countries);
  38.     });
  39. }
  40.  
  41. function initializeTowns() {
  42.     var townsDropdown = document.createElement('select');
  43.     townsDropdown.setAttribute('onclick', 'javascript: generateTowns(this);');
  44.     $.ajax({
  45.         method: 'GET',
  46.         headers: headers,
  47.         url: 'https://api.parse.com/1/classes/Country'
  48.     }).success(function (data) {
  49.         for (var country in data.results) {
  50.             var currentCountryOption = $('<option></option>')
  51.                 .text(data.results[country].name)
  52.                 .val(data.results[country].objectId);
  53.  
  54.             townsDropdown = $(townsDropdown).append(currentCountryOption);
  55.         }
  56.     });
  57.  
  58.     $('#townsContainer').before(townsDropdown);
  59. }
  60.  
  61. function generateTowns(target) {
  62.     if (target.selectedIndex !== currentTownIndex) {
  63.         currentTownIndex = target.selectedIndex;
  64.         var countryId = target.options[target.selectedIndex].value;
  65.         var whereParameter = '{' +
  66.             '"country":' +
  67.                 '{"__type":"Pointer","className":"Country","objectId":"' + countryId + '"}' +
  68.             '}';
  69.  
  70.         $.ajax({
  71.             method: 'GET',
  72.             headers: headers,
  73.             url: 'https://api.parse.com/1/classes/Town?where=' + whereParameter
  74.         }).success(function (data) {
  75.             var townsList = $('#townsList');
  76.             for (var town in data.results) {
  77.                 townsList.append('<li></li>').text(data.results[town].name);
  78.             }
  79.         });
  80.     }
  81. }
  82.  
  83. function addTown() {
  84.     var townName = window.prompt('Enter the town\'s name');
  85.     var countryName = window.prompt('Enter the country name it belongs to');
  86.     var whereParameter = '{"name":"' + countryName + '"}';
  87.  
  88.     $.ajax({
  89.         method: 'GET',
  90.         headers: headers,
  91.         url: 'https://api.parse.com/1/classes/Country?where=' + whereParameter
  92.     }).success(function(data) {
  93.         if (data.results.length > 0) {
  94.             console.log('yes');
  95.         }
  96.     });
  97. }
  98.  
  99. function deleteCountry(id) {
  100.     console.log('https://api.parse.com/1/classes/Country/' + id);
  101.     $.ajax({
  102.         method: 'DELETE',
  103.         headers: headers,
  104.         url: 'https://api.parse.com/1/classes/Country/' + id
  105.     }).success(function() {
  106.         window.location.replace(window.location.href);
  107.     });
  108. }
  109.  
  110. function editCountry(id) {
  111.     var newName = window.prompt("Enter the country's new name");
  112.  
  113.     $.ajax({
  114.         method: 'PUT',
  115.         headers: headers,
  116.         url: 'https://api.parse.com/1/classes/Country/' + id,
  117.         data: JSON.stringify({
  118.             'name': newName
  119.         })
  120.     }).success(function() {
  121.         window.location.replace(window.location.href);
  122.     });
  123. }
  124.  
  125. function newCountry() {
  126.     var name = window.prompt('Enter the country\'s name');
  127.  
  128.     $.ajax({
  129.         method: 'POST',
  130.         headers: headers,
  131.         url: 'https://api.parse.com/1/classes/Country',
  132.         data: JSON.stringify({
  133.             'name': name
  134.         })
  135.     }).success(function() {
  136.         window.location.replace(window.location.href);
  137.     });
  138. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement