bookslist.controller.js 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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. booksRepository.getBooks()
  46. .then(function (response)
  47. {
  48. $scope.books = response.data;
  49. }, function (error)
  50. {
  51. alert(error);
  52. }
  53. );
  54. }
  55. ]);
  56. }
  57. )();