Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- initializeCountries();
- initializeTowns();
- var currentTownIndex = 0;
- var headers = {
- 'X-Parse-Application-Id': 'cDOllTGD0mgH2eJVkoAEQX8Kl5uG9V5BOF99Rxz0',
- 'X-Parse-REST-API-Key': 'Chn6ZbJZKOcaI42fa2wQdrgoMZSBg88eWJmeOmVd'
- };
- function initializeCountries() {
- $.ajax({
- method: 'GET',
- headers: headers,
- url: 'https://api.parse.com/1/classes/Country'
- }).success(function (data) {
- var countriesContainer = $('#countriesContainer');
- var countries = $('<ul></ul>');
- for (var country in data.results) {
- var liElement = $('<li></li>').text(data.results[country].name);
- var deleteButton = document.createElement('button');
- deleteButton.innerText = 'Delete';
- deleteButton.setAttribute('onclick',
- 'javascript: deleteCountry("' + data.results[country].objectId + '")');
- var editButton = document.createElement('button');
- editButton.innerText = 'Edit';
- editButton.setAttribute('onclick',
- 'javascript: editCountry("' + data.results[country].objectId + '")');
- liElement.append(deleteButton);
- liElement.append(editButton);
- countries.append(liElement);
- }
- countriesContainer.append(countries);
- });
- }
- function initializeTowns() {
- var townsDropdown = document.createElement('select');
- townsDropdown.setAttribute('onclick', 'javascript: generateTowns(this);');
- $.ajax({
- method: 'GET',
- headers: headers,
- url: 'https://api.parse.com/1/classes/Country'
- }).success(function (data) {
- for (var country in data.results) {
- var currentCountryOption = $('<option></option>')
- .text(data.results[country].name)
- .val(data.results[country].objectId);
- townsDropdown = $(townsDropdown).append(currentCountryOption);
- }
- });
- $('#townsContainer').before(townsDropdown);
- }
- function generateTowns(target) {
- if (target.selectedIndex !== currentTownIndex) {
- currentTownIndex = target.selectedIndex;
- var countryId = target.options[target.selectedIndex].value;
- var whereParameter = '{' +
- '"country":' +
- '{"__type":"Pointer","className":"Country","objectId":"' + countryId + '"}' +
- '}';
- $.ajax({
- method: 'GET',
- headers: headers,
- url: 'https://api.parse.com/1/classes/Town?where=' + whereParameter
- }).success(function (data) {
- var townsList = $('#townsList');
- for (var town in data.results) {
- townsList.append('<li></li>').text(data.results[town].name);
- }
- });
- }
- }
- function addTown() {
- var townName = window.prompt('Enter the town\'s name');
- var countryName = window.prompt('Enter the country name it belongs to');
- var whereParameter = '{"name":"' + countryName + '"}';
- $.ajax({
- method: 'GET',
- headers: headers,
- url: 'https://api.parse.com/1/classes/Country?where=' + whereParameter
- }).success(function(data) {
- if (data.results.length > 0) {
- console.log('yes');
- }
- });
- }
- function deleteCountry(id) {
- console.log('https://api.parse.com/1/classes/Country/' + id);
- $.ajax({
- method: 'DELETE',
- headers: headers,
- url: 'https://api.parse.com/1/classes/Country/' + id
- }).success(function() {
- window.location.replace(window.location.href);
- });
- }
- function editCountry(id) {
- var newName = window.prompt("Enter the country's new name");
- $.ajax({
- method: 'PUT',
- headers: headers,
- url: 'https://api.parse.com/1/classes/Country/' + id,
- data: JSON.stringify({
- 'name': newName
- })
- }).success(function() {
- window.location.replace(window.location.href);
- });
- }
- function newCountry() {
- var name = window.prompt('Enter the country\'s name');
- $.ajax({
- method: 'POST',
- headers: headers,
- url: 'https://api.parse.com/1/classes/Country',
- data: JSON.stringify({
- 'name': name
- })
- }).success(function() {
- window.location.replace(window.location.href);
- });
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement