Advertisement
Guest User

Untitled

a guest
Apr 15th, 2015
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var APPLICATION_ID = 'oQ8aDpqIgZlWv4i42BfByjQp0ABeKcm8LkDlcdbX';
  2. var REST_API_KEY = '6K0s7FdX5ZQz8alsxn0xa53PF1Wuurl53NEbeBor';
  3.  
  4. function getBooks () {
  5.     $.ajax({
  6.         method: 'GET',
  7.         headers: {
  8.             'X-Parse-Application-Id': APPLICATION_ID,
  9.             'X-Parse-REST-API-Key': REST_API_KEY
  10.         },
  11.         url: 'https://api.parse.com/1/classes/Book',
  12.         success: listBooks,
  13.         error: error
  14.     });
  15. };
  16.  
  17. function listBooks (data) {
  18.     var booksList = $('<ul></ul>'),
  19.         book = {};
  20.  
  21.     $('body').empty();
  22.     booksList.appendTo('body');
  23.  
  24.     for(book in data.results) {
  25.         $('<li></li>')
  26.             .text('Title: ' + data.results[book].title + ', author: ' + data.results[book].author +', isbn: ' +
  27.                 data.results[book].isbn)
  28.             .data('book', data.results[book])
  29.             .click(showDataInHTMLForm)
  30.             .appendTo(booksList);
  31.     }
  32. };
  33.  
  34. function showDataInHTMLForm () {
  35.     var book = $(this).data('book');
  36.  
  37.     $('<form></form>')
  38.         .append($('<label></label>').text('Title: ' + book.title))
  39.         .append($('<label></label>').text('Author: ' + book.author))
  40.         .append($('<label></label>').text('Isbn: ' + book.isbn))
  41.         .append($('<button></button>').text('Edit book').click(editBook(book.objectId)))
  42.         .appendTo('body');
  43. };
  44.  
  45. function editBook (bookObjectId) {
  46.     var newBookTitle = prompt('New book title:');
  47.         newBookAuthor = prompt('new book author:');
  48.         newBookIsdn = prompt('new book isdn:');
  49.  
  50.     $.ajax({
  51.         method: 'PUT',
  52.         headers: {
  53.             'X-Parse-Application-Id': APPLICATION_ID,
  54.             'X-Parse-REST-API-Key': REST_API_KEY
  55.         },
  56.         data: JSON.stringify({
  57.             'title': newBookTitle,
  58.             'author': newBookAuthor,
  59.             'isbn': newBookIsdn
  60.         }),
  61.         url: 'https://api.parse.com/1/classes/Book/' + bookObjectId,
  62.         success: getBooks,
  63.         error: error
  64.     });
  65. };
  66.  
  67. function error (err) {
  68.     throw new Error('AJAX request failure.');
  69. };
  70.  
  71. getBooks();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement