bookslist.controller.js 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. (function ()
  2. {
  3. 'use strict';
  4. app.controller('BooksList', ['$scope', 'books.repository', '$uibModal', function ($scope, booksRepository, $uibModal)
  5. {
  6. $scope.sortField = 'title';
  7. $scope.addBook = function(){
  8. var modalInstance = $uibModal.open({
  9. templateUrl : 'app/modals/add-book.template.html',
  10. controller: 'AddBook',
  11. size: 'lg'
  12. })
  13. modalInstance.result
  14. .then(function(data){
  15. booksRepository.addBook(data);
  16. $scope.books.push(data)
  17. }, function(){})
  18. }
  19. $scope.sortBy = function (field)
  20. {
  21. if ($scope.sortField == field)
  22. {
  23. $scope.sortField = '-' + field;
  24. }
  25. else
  26. {
  27. $scope.sortField = field;
  28. }
  29. };
  30. $scope.deleteBook = function (book){
  31. var modalInstance = $uibModal.open({
  32. templateUrl: 'app/modals/confirm/confirm.template.html',
  33. controller: 'Confirm',
  34. size: 'sm'
  35. });
  36. modalInstance.result
  37. .then(function(result){
  38. if(!result) return;
  39. booksRepository.deleteBook(book.id)
  40. .then(function(response){
  41. $scope.books.splice($scope.books.indexOf(book), 1)
  42. })
  43. })
  44. };
  45. var searchResult = sessionStorage.getItem('searchResult');
  46. if(searchResult){
  47. $scope.books = JSON.parse(searchResult);
  48. sessionStorage.removeItem('searchResult')
  49. }else {
  50. booksRepository.getBooks()
  51. .then(function (response)
  52. {
  53. $scope.books = response.data;
  54. }, function (error)
  55. {
  56. alert(error);
  57. }
  58. );
  59. }
  60. $scope.$on('search', function(event, data){
  61. $scope.books = data;
  62. });
  63. }
  64. ]);
  65. }
  66. )();