sanjiisan

Untitled

Jun 10th, 2017
50
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.38 KB | None | 0 0
  1. $(function () {
  2. var $form = $('#bookAdd');
  3. var $bookList = $('#booksList');
  4.  
  5. $('body').on('click', '.btn-book-remove', function () {
  6. var id = $(this).data('id');
  7. var that = $(this);
  8.  
  9. $
  10. .ajax({
  11. url: '../rest/rest.php/book/' + id,
  12. type: 'DELETE'
  13. })
  14. .done(function (response) {
  15. that.closest('.list-group-item').remove();
  16. })
  17. .fail(function (error) {
  18. console.log('Delete book error', error);
  19. });
  20. });
  21.  
  22. $('body').on('click', '.btn-book-show-description', function () {
  23. var id = $(this).data('id');
  24. var that = $(this);
  25.  
  26. $
  27. .ajax({
  28. url: '../rest/rest.php/book/' + id,
  29. type: 'GET'
  30. })
  31. .done(function (response) {
  32. var descElement = that.closest('.list-group-item').find('.book-description');
  33.  
  34. descElement.text(response.success[0].description);
  35. descElement.slideDown();
  36. })
  37. .fail(function (error) {
  38. console.log('Create book error', error);
  39. });
  40. });
  41.  
  42. function getBooks() {
  43. $
  44. .ajax({
  45. url: '../rest/rest.php/book',
  46. type: 'GET'
  47. })
  48. .done(function (response) {
  49. for (var i = 0; i < response.success.length; i++) {
  50. renderBook(response.success[i]);
  51. }
  52. })
  53. .fail(function (error) {
  54. console.log('Create book error', error);
  55. });
  56. }
  57.  
  58. function renderBook(book) {
  59. var string = `<li class="list-group-item">
  60. <div class="panel-heading">
  61. <span class="bookTitle">${book.title}</span>
  62. <button data-id="${book.id}"
  63. class="btn btn-danger pull-right btn-xs btn-book-remove"><i
  64. class="fa fa-trash"></i>
  65. </button>
  66. <button data-id="${book.id}"
  67. class="btn btn-primary pull-right btn-xs btn-book-show-description"><i
  68. class="fa fa-info-circle"></i>
  69. </button>
  70. </div>
  71. <div class="panel-body book-description">
  72. </div>
  73. </li>`;
  74.  
  75. $bookList.html($bookList.html() + string);
  76. }
  77.  
  78. $form.on('submit', function (event) {
  79. event.preventDefault();
  80.  
  81. var title = $('#title').val(),
  82. description = $('#description').val();
  83.  
  84. var newBook = {
  85. title: title,
  86. description: description
  87. };
  88.  
  89. $
  90. .ajax({
  91. url: '../rest/rest.php/book',
  92. type: 'POST',
  93. dataType: 'json',
  94. data: newBook
  95. })
  96. .done(function (response) {
  97. renderBook(response.success[0]);
  98. })
  99. .fail(function (error) {
  100. console.log('Create book error', error);
  101. });
  102. });
  103.  
  104. getBooks();
  105. });
Add Comment
Please, Sign In to add comment