bookdetails.controller.js 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. (function ()
  2. {
  3. 'use strict';
  4. app.controller('BookDetails', ['$scope', 'books.repository', '$routeParams','utils', function ($scope, booksRepository, $routeParams, utils)
  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. utils.notify({
  14. message: error.statusText,
  15. type: 'danger'
  16. })
  17. }
  18. )
  19. $scope.isEditeMode = false;
  20. $scope.edite = function(){
  21. $scope.isEditeMode = true;
  22. details = angular.copy($scope.details)
  23. }
  24. $scope.noEdite = function(){
  25. $scope.isEditeMode = false;
  26. $scope.details = angular.copy(details)
  27. }
  28. booksRepository.getAuthors()
  29. .then(function(response){
  30. $scope.authors = response.data.map(function(author){
  31. return {
  32. id: author.id,
  33. name: author.firstname + ' ' + author.lastname
  34. }
  35. })
  36. })
  37. $scope.save = function(){
  38. booksRepository.updateBookById($scope.details.id, $scope.details)
  39. $scope.isEditeMode = false;
  40. };
  41. $scope.getAuthorsById = function(id){
  42. if(!$scope.authors || !id) return;
  43. return $scope.authors.filter(function(item){
  44. return item.id === id;
  45. })[0].name;
  46. }
  47. }
  48. ]);
  49. }
  50. )();