From bda2c90e3df68233f86226bcb574526c8abc8266 Mon Sep 17 00:00:00 2001 From: Alexis Lahouze Date: Thu, 13 Jul 2017 17:14:20 +0200 Subject: [PATCH] Change account controller into class. --- src/accounts/account.controller.ts | 56 ++++++++++++++++-------------- src/accounts/index.ts | 2 +- 2 files changed, 31 insertions(+), 27 deletions(-) diff --git a/src/accounts/account.controller.ts b/src/accounts/account.controller.ts index 73ea57c..cdccb7a 100644 --- a/src/accounts/account.controller.ts +++ b/src/accounts/account.controller.ts @@ -1,14 +1,21 @@ var accountFormTmpl = require('./account.form.tmpl.html'), accountDeleteTmpl = require('./account.delete.tmpl.html'); -module.exports = function(Account, AccountBalances, Notification, $log, $modal) { - var vm = this; +export class AccountController { + 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 * overdraft. */ - vm.rowClass = function(account) { + rowClass(account) { // eslint-disable-next-line camelcase if (!account || !account.authorized_overdraft || !account.current) { return; @@ -25,7 +32,7 @@ module.exports = function(Account, AccountBalances, Notification, $log, $modal) /* * Return the class for a value compared to account authorized overdraft. */ - vm.valueClass = function(account, value) { + valueClass(account, value) { if (!account || !value) { return; } @@ -38,10 +45,10 @@ module.exports = function(Account, AccountBalances, Notification, $log, $modal) } }; - vm.load = function() { + load() { vm.accounts = Account.query({}, function(result) { return result.map(function(item) { - item.balances = AccountBalances.get({id: item.id}); + item.balances = this.AccountBalances.get({id: item.id}); return item; }); }); @@ -50,39 +57,39 @@ module.exports = function(Account, AccountBalances, Notification, $log, $modal) /* * Add an empty account. */ - vm.add = function() { + add() { var account = new Account({ // eslint-disable-next-line camelcase authorized_overdraft: 0 }); // Insert account at the begining of the array. - return vm.modify(account); + return this.modify(account); }; /* * Save account. */ - vm.save = function(account) { + save(account) { 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; }, 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 ); }); }; - vm.confirmDelete = function(account) { + confirmDelete(account) { var title = "Delete account #" + account.id; - $modal({ + this.$modal({ templateUrl: accountDeleteTmpl, controller: function($scope, title, account, $delete) { $scope.title = title; @@ -95,7 +102,7 @@ module.exports = function(Account, AccountBalances, Notification, $log, $modal) locals: { title: title, account: account, - $delete: vm.delete + $delete: this.delete } }); }; @@ -103,17 +110,17 @@ module.exports = function(Account, AccountBalances, Notification, $log, $modal) /* * Delete an account. */ - vm.delete = function(account) { + delete(account) { var id = account.id; return account.$delete().then(function() { - Notification.success('account #' + id + ' deleted.'); + this.Notification.success('account #' + id + ' deleted.'); - vm.load(); + this.load(); return account; }, function(result) { - Notification.error( + this.Notification.error( 'An error occurred while trying to delete account #' + id + ':
' + result ); @@ -123,7 +130,7 @@ module.exports = function(Account, AccountBalances, Notification, $log, $modal) /* * Open the popup to modify the account, save it on confirm. */ - vm.modify = function(account) { + modify(account) { // FIXME Alexis Lahouze 2017-06-15 i18n var title = "Account"; @@ -131,7 +138,7 @@ module.exports = function(Account, AccountBalances, Notification, $log, $modal) title = title + " #" + account.id; } - $modal({ + this.$modal({ templateUrl: accountFormTmpl, controller: function($scope, title, account, $save) { $scope.title = title; @@ -146,11 +153,8 @@ module.exports = function(Account, AccountBalances, Notification, $log, $modal) locals: { title: title, account: account, - $save: vm.save + $save: this.save } }); }; - - // Load accounts. - vm.load(); }; diff --git a/src/accounts/index.ts b/src/accounts/index.ts index b00f3ae..57db7ef 100644 --- a/src/accounts/index.ts +++ b/src/accounts/index.ts @@ -28,7 +28,7 @@ var ngResource = require('angular-resource'), var AccountFactory = require('./account.factory'); var AccountBalancesFactory = require('./accountBalances.factory'); var AccountConfig = require('./account.config'); -var AccountController = require('./account.controller'); +import { AccountController } from './account.controller'; export default angular.module('accountant.accounts', [ ngResource,