bookdetails.controller.js 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. (function ()
  2. {
  3. 'use strict';
  4. app.controller('BookDetails', ['$scope', 'books.repository', '$routeParams', function ($scope, booksRepository, $routeParams)
  5. {
  6. var details = {};
  7. booksRepository.getBookById($routeParams.id)
  8. .then(function(response) {
  9. $scope.details = response.data;
  10. $scope.details.date = new Date($scope.details.date)
  11. console.log($scope.details)
  12. }, function(error) {
  13. alert(error)}
  14. )
  15. $scope.isEditeMode = false;
  16. $scope.edite = function(){
  17. $scope.isEditeMode = true;
  18. details = angular.copy($scope.details)
  19. }
  20. $scope.noEdite = function(){
  21. $scope.isEditeMode = false;
  22. $scope.details = angular.copy(details)
  23. }
  24. booksRepository.getAuthors()
  25. .then(function(response){
  26. $scope.authors = response.data.map(function(author){
  27. return {
  28. id: author.id,
  29. name: author.firstname + ' ' + author.lastname
  30. }
  31. })
  32. })
  33. $scope.save = function(){
  34. booksRepository.updateBookById($scope.details.id, $scope.details)
  35. $scope.isEditeMode = false;
  36. };
  37. $scope.getAuthorsById = function(id){
  38. if(!$scope.authors || !id) return;
  39. return $scope.authors.filter(function(item){
  40. return item.id === id;
  41. })[0].name;
  42. }
  43. }
  44. ]);
  45. }
  46. )();