1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162 |
- (function() {
- 'use strict';
- app.controller('Authors', ['$scope', 'books.repository', '$uibModal', 'utils', '$timeout', function($scope, booksRepository, $uibModal, utils, $timeout) {
- $scope.authors = [];
- booksRepository.getAuthors().then(function(response) {
- $scope.authors = response.data;
- }, function(error) {
- utils.notify({message: error.data.message, type: 'danger'});
- });
- $scope.deleteAuthor = function(authorId) {
- var modalInstance = $uibModal.open({
- templateUrl: 'app/modals/confirm/add-book.template.html',
- controller: 'Confirm',
- size: 'sm'
- });
- modalInstance.result.then(function(result) {
- if (!result) return;
- var author = $scope.authors.filter(function(author) { return author.id === authorId })[0],
- index = $scope.authors.indexOf(author);
- booksRepository.deleteAuthor(authorId).then(function(response) {
- $scope.authors.splice( index, 1 );
- utils.notify({message: 'Author has been successfully deleted', type: 'success'});
- }, function(error) {});
- }, function() {});
- };
- $scope.addAuthor = function() {
- $scope.inserted = {
- id: 0,
- firstname: 'Вася',
- lastname: 'Пупкин'
- };
-
- $scope.authors.push($scope.inserted);
- };
- $scope.saveAuthor = function(data, authorId) {
- if (authorId) {
- booksRepository.updateAuthorById(authorId, data).then(function(response) {
- utils.notify({message: 'Author successfully updated', type: 'success'});
- }, function(error) {});
- } else {
- booksRepository.addAuthor(data).then(function(response) {
- $scope.authors[$scope.authors.length - 1] = response.data;
- utils.notify({message: 'Author successfully added', type: 'success'});
- }, function(error) {});
- }
- };
- $scope.deleteFromArray = function(author, editableForm) {
- if (!author.id) {
- $scope.authors.splice($scope.authors.indexOf(author), 1);
- editableForm.$removeEditable();
- } else {
- editableForm.$cancel();
- }
- };
- }]);
- })();
|