Guest User

Script

a guest
Oct 29th, 2015
37
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. $(function(){
  2.   var PARSE_APP_ID = "KmgKIBTeOp0vAuNpqAhfskNfgBGTdEaUnaXwDOoY";
  3.   var PARSE_REST_API_KEY = "ZUfcs4gvWlHX6A556buvZgLne6M7swy6DdKeFhR0";
  4.  
  5.   loadCountries();
  6.  
  7.   function loadCountries(){
  8.     $.ajax({
  9.       method : 'get',
  10.       headers : {
  11.         'X-Parse-Application-Id' : PARSE_APP_ID,
  12.         'X-Parse-REST-API-Key' : PARSE_REST_API_KEY,
  13.         'Content-type' : 'application/json'
  14.       },
  15.       url : 'https://api.parse.com/1/classes/Country',
  16.       success : countryLoaded,
  17.       error : error
  18.     });
  19.   }
  20.  
  21.   function countryLoaded(data){
  22.     $('#output').empty();
  23.     for ( var c in data.results ) {
  24.       var country = data.results[c];
  25.       var countryUl = $('<ul></ul>');
  26.  
  27.       var addCityBtn = $('<span id="add-city">+</span>');
  28.       addCityBtn.data('country' , country);
  29.       addCityBtn.click(addCity);
  30.  
  31.       var removeBtn = $('<span id="remove-country">▼</span>');
  32.       removeBtn.data('country' , country);
  33.       removeBtn.click(removeCountry);
  34.  
  35.       //var renameBtn = $('<span id="rename-country">▲</span>');
  36.       //renameBtn.data('country' , country);
  37.       //renameBtn.click(renameCountry);
  38.  
  39.  
  40.       countryUl.data('country' , country);
  41.       countryUl.text(country.name);
  42.       countryUl.addClass(country.name);
  43.       countryUl.append(removeBtn)
  44.         .append(addCityBtn)
  45.         //.append(renameBtn);
  46.       countryUl.appendTo($('#output'));
  47.  
  48.       countryUl.dblclick(renameCountry);
  49.     }
  50.   }
  51.  
  52.   $('#add-new-country').click(addCountry);
  53.  
  54.   function addCountry(){
  55.     var newCountryName = $('#country-input').val();
  56.     $.ajax({
  57.       method : 'post',
  58.       headers : {
  59.         'X-Parse-Application-Id' : PARSE_APP_ID,
  60.         'X-Parse-REST-API-Key' : PARSE_REST_API_KEY,
  61.         'Content-type' : 'application/json'
  62.       },
  63.       data : JSON.stringify(
  64.         {
  65.           "name" : newCountryName
  66.         }
  67.       ),
  68.       url : 'https://api.parse.com/1/classes/Country',
  69.       success : loadCountries,
  70.       error : error
  71.     });
  72.   }
  73.  
  74.   function addCity(){
  75.     var townName = prompt("Enter name of the city");
  76.     var country = $(this).data('country');
  77.  
  78.     $.ajax({
  79.       method : 'post',
  80.       headers : {
  81.         'X-Parse-Application-Id' : PARSE_APP_ID,
  82.         'X-Parse-REST-API-Key' : PARSE_REST_API_KEY,
  83.         'Content-type' : 'application/json'
  84.       },
  85.       url : 'https://api.parse.com/1/classes/City/',
  86.       data: JSON.stringify(
  87.         {
  88.           "name": townName,
  89.           "country":
  90.           {
  91.             "__type": "Pointer",
  92.             "className": "Country",
  93.             "objectId": country.objectId
  94.           }
  95.         }
  96.       ),
  97.       success : loadCountries,
  98.       error : error
  99.     });
  100.   }
  101.  
  102.   function renameCountry(){
  103.     var country = $(this).data('country');
  104.     var oldCountryName = country.name;
  105.     var newCountryName = prompt("Rename country," + oldCountryName) || oldCountryName;
  106.  
  107.     $.ajax({
  108.       method : 'put',
  109.       headers : {
  110.         'X-Parse-Application-Id' : PARSE_APP_ID,
  111.         'X-Parse-REST-API-Key' : PARSE_REST_API_KEY,
  112.         'Content-type' : 'application/json'
  113.       },
  114.       data : JSON.stringify
  115.       (
  116.         {
  117.           "name" : newCountryName
  118.         }
  119.       ),
  120.       url : 'https://api.parse.com/1/classes/Country' + country.objectId,
  121.       success : loadCountries,
  122.       error : error
  123.     });
  124.   }
  125.  
  126.   function removeCountry(){
  127.     var country = $(this).data('country');
  128.     $.ajax({
  129.       method : "delete",
  130.       headers : {
  131.         'X-Parse-Application-Id' : PARSE_APP_ID,
  132.         'X-Parse-REST-API-Key' : PARSE_REST_API_KEY,
  133.         'Content-type' : 'application/json'
  134.       },
  135.       url : "https://api.parse.com/1/classes/Country/" + country.objectId,
  136.       success : loadCountries,
  137.       error : error
  138.     });
  139.   }
  140.  
  141.   function addCity(){
  142.     var cityName = prompt("Enter name of the city");
  143.     var country = $(this).data('country');
  144.  
  145.     $.ajax({
  146.       method : 'post',
  147.       headers : {
  148.         'X-Parse-Application-Id' : PARSE_APP_ID,
  149.         'X-Parse-REST-API-Key' : PARSE_REST_API_KEY,
  150.         'Content-type' : 'application/json'
  151.       },
  152.       url : "https://api.parse.com/1/classes/City/",
  153.       data : JSON.stringify(
  154.         {
  155.           "name" : cityName,
  156.           "country" :
  157.           {
  158.             "__type" : "Pointer",
  159.             "className" : "Country",
  160.             "objectId" : country.objectId
  161.           }
  162.         }
  163.       ),
  164.       success : loadCountries,
  165.       error : error
  166.     });
  167.   }
  168.  
  169.   $('#country-input').on('keyup' , function(){
  170.     var visibility = $('p').css('display');
  171.     var value;
  172.     var $countries;
  173.     value = $(this).val().toLowerCase();
  174.     $countries = $('p');
  175.     if (value === '') {
  176.       $countries.show();
  177.     } else {
  178.       $countries
  179.         .hide()
  180.         .filter(function (_, country) {
  181.           return $(country).html().toLowerCase().indexOf(value.toLowerCase()) !== -1;
  182.         })
  183.         .show();
  184.     }
  185.   });
  186.  
  187.   function error(err){
  188.     console.log("An error appeard");
  189.   }
  190. });
RAW Paste Data

Adblocker detected! Please consider disabling it...

We've detected AdBlock Plus or some other adblocking software preventing Pastebin.com from fully loading.

We don't have any obnoxious sound, or popup ads, we actively block these annoying types of ads!

Please add Pastebin.com to your ad blocker whitelist or disable your adblocking software.

×