accounts.controller.js 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. (function(){
  2. 'use strict';
  3. angular
  4. .module('cloudbudget')
  5. .controller('AccountsController', AccountsController);
  6. AccountsController.$inject = ['$scope', '$location', '$rootScope', 'FlashService', 'AccountsService'];
  7. function AccountsController($scope, $location, $rootScope, FlashService, AccountsService) {
  8. var vm = this;
  9. vm.dataLoading = false;
  10. vm.accounts = [];
  11. vm.create = create;
  12. vm.drop = drop;
  13. vm.edit = edit;
  14. vm.consult = consult;
  15. (function init() {
  16. vm.dataLoading = true;
  17. AccountsService.list()
  18. .then(function(response) {
  19. if( response.success ) {
  20. vm.accounts = response.accounts;
  21. } else {
  22. FlashService.error(response.message);
  23. }
  24. vm.dataLoading = false;
  25. })
  26. })();
  27. function create() {
  28. vm.dataLoading = true;
  29. AccountsService.create(vm.account)
  30. .then( function(response) {
  31. if( response.success) {
  32. vm.accounts.push(response.account);
  33. } else {
  34. FlashService.error(response.message);
  35. }
  36. vm.dataLoading = false;
  37. });
  38. vm.account = angular.copy({});
  39. $scope.form.$setPristine();
  40. };
  41. function drop(account) {
  42. vm.dataLoading = true;
  43. AccountsService.drop(account)
  44. .then(function(response) {
  45. if( response.success ) {
  46. var index = vm.accounts.indexOf(account);
  47. vm.accounts.splice(index, 1);
  48. } else {
  49. FlashService.error( response.message );
  50. }
  51. vm.dataLoading = false;
  52. });
  53. };
  54. function edit(altered, origin) {
  55. vm.dataLoading = true;
  56. return AccountsService.edit(origin._id, altered)
  57. .then( function(response) {
  58. if( response.success ) {
  59. var index = vm.accounts.map(function (item) {
  60. return item._id;
  61. }).indexOf(origin._id);
  62. vm.accounts[index] = response.account;
  63. } else {
  64. FlashService.error( response.message );
  65. return false;
  66. }
  67. })
  68. };
  69. function consult(account) {
  70. $location.path('/account/' + account._id);
  71. };
  72. }
  73. })();