Change account controller into class.

This commit is contained in:
Alexis Lahouze 2017-07-13 17:14:20 +02:00
parent 995af2fbd3
commit bda2c90e3d
2 changed files with 31 additions and 27 deletions

View File

@ -1,14 +1,21 @@
var accountFormTmpl = require('./account.form.tmpl.html'), var accountFormTmpl = require('./account.form.tmpl.html'),
accountDeleteTmpl = require('./account.delete.tmpl.html'); accountDeleteTmpl = require('./account.delete.tmpl.html');
module.exports = function(Account, AccountBalances, Notification, $log, $modal) { export class AccountController {
var vm = this; static $inject = ['Account', 'AccountBalances', 'Notification', '$log', '$modal'];
accounts: Account[];
constructor(private Account, private AccountBalances, private Notification, private $log, private $modal) {
// Load accounts.
this.load();
}
/* /*
* Return the class for an account current value compared to authorized * Return the class for an account current value compared to authorized
* overdraft. * overdraft.
*/ */
vm.rowClass = function(account) { rowClass(account) {
// eslint-disable-next-line camelcase // eslint-disable-next-line camelcase
if (!account || !account.authorized_overdraft || !account.current) { if (!account || !account.authorized_overdraft || !account.current) {
return; return;
@ -25,7 +32,7 @@ module.exports = function(Account, AccountBalances, Notification, $log, $modal)
/* /*
* Return the class for a value compared to account authorized overdraft. * Return the class for a value compared to account authorized overdraft.
*/ */
vm.valueClass = function(account, value) { valueClass(account, value) {
if (!account || !value) { if (!account || !value) {
return; return;
} }
@ -38,10 +45,10 @@ module.exports = function(Account, AccountBalances, Notification, $log, $modal)
} }
}; };
vm.load = function() { load() {
vm.accounts = Account.query({}, function(result) { vm.accounts = Account.query({}, function(result) {
return result.map(function(item) { return result.map(function(item) {
item.balances = AccountBalances.get({id: item.id}); item.balances = this.AccountBalances.get({id: item.id});
return item; return item;
}); });
}); });
@ -50,39 +57,39 @@ module.exports = function(Account, AccountBalances, Notification, $log, $modal)
/* /*
* Add an empty account. * Add an empty account.
*/ */
vm.add = function() { add() {
var account = new Account({ var account = new Account({
// eslint-disable-next-line camelcase // eslint-disable-next-line camelcase
authorized_overdraft: 0 authorized_overdraft: 0
}); });
// Insert account at the begining of the array. // Insert account at the begining of the array.
return vm.modify(account); return this.modify(account);
}; };
/* /*
* Save account. * Save account.
*/ */
vm.save = function(account) { save(account) {
return account.$save().then(function(data) { return account.$save().then(function(data) {
Notification.success('Account #' + data.id + ' saved.'); this.Notification.success('Account #' + data.id + ' saved.');
vm.load(); this.load();
return data; return data;
}, function(result){ }, function(result){
$log.error('Error while saving account', account, result); this.$log.error('Error while saving account', account, result);
Notification.error( this.Notification.error(
'Error while saving account: ' + result.message 'Error while saving account: ' + result.message
); );
}); });
}; };
vm.confirmDelete = function(account) { confirmDelete(account) {
var title = "Delete account #" + account.id; var title = "Delete account #" + account.id;
$modal({ this.$modal({
templateUrl: accountDeleteTmpl, templateUrl: accountDeleteTmpl,
controller: function($scope, title, account, $delete) { controller: function($scope, title, account, $delete) {
$scope.title = title; $scope.title = title;
@ -95,7 +102,7 @@ module.exports = function(Account, AccountBalances, Notification, $log, $modal)
locals: { locals: {
title: title, title: title,
account: account, account: account,
$delete: vm.delete $delete: this.delete
} }
}); });
}; };
@ -103,17 +110,17 @@ module.exports = function(Account, AccountBalances, Notification, $log, $modal)
/* /*
* Delete an account. * Delete an account.
*/ */
vm.delete = function(account) { delete(account) {
var id = account.id; var id = account.id;
return account.$delete().then(function() { return account.$delete().then(function() {
Notification.success('account #' + id + ' deleted.'); this.Notification.success('account #' + id + ' deleted.');
vm.load(); this.load();
return account; return account;
}, function(result) { }, function(result) {
Notification.error( this.Notification.error(
'An error occurred while trying to delete account #' + 'An error occurred while trying to delete account #' +
id + ':<br />' + result id + ':<br />' + result
); );
@ -123,7 +130,7 @@ module.exports = function(Account, AccountBalances, Notification, $log, $modal)
/* /*
* Open the popup to modify the account, save it on confirm. * Open the popup to modify the account, save it on confirm.
*/ */
vm.modify = function(account) { modify(account) {
// FIXME Alexis Lahouze 2017-06-15 i18n // FIXME Alexis Lahouze 2017-06-15 i18n
var title = "Account"; var title = "Account";
@ -131,7 +138,7 @@ module.exports = function(Account, AccountBalances, Notification, $log, $modal)
title = title + " #" + account.id; title = title + " #" + account.id;
} }
$modal({ this.$modal({
templateUrl: accountFormTmpl, templateUrl: accountFormTmpl,
controller: function($scope, title, account, $save) { controller: function($scope, title, account, $save) {
$scope.title = title; $scope.title = title;
@ -146,11 +153,8 @@ module.exports = function(Account, AccountBalances, Notification, $log, $modal)
locals: { locals: {
title: title, title: title,
account: account, account: account,
$save: vm.save $save: this.save
} }
}); });
}; };
// Load accounts.
vm.load();
}; };

View File

@ -28,7 +28,7 @@ var ngResource = require('angular-resource'),
var AccountFactory = require('./account.factory'); var AccountFactory = require('./account.factory');
var AccountBalancesFactory = require('./accountBalances.factory'); var AccountBalancesFactory = require('./accountBalances.factory');
var AccountConfig = require('./account.config'); var AccountConfig = require('./account.config');
var AccountController = require('./account.controller'); import { AccountController } from './account.controller';
export default angular.module('accountant.accounts', [ export default angular.module('accountant.accounts', [
ngResource, ngResource,