Separate Account balances in another factory.

This commit is contained in:
Alexis Lahouze 2017-07-10 08:17:11 +02:00
parent 4abe5092ec
commit d4eaa1454c
5 changed files with 25 additions and 26 deletions

View File

@ -1,7 +1,7 @@
var accountFormTmpl = require('./account.form.tmpl.html'),
accountDeleteTmpl = require('./account.delete.tmpl.html');
module.exports = function(Account, Notification, $log, $modal) {
module.exports = function(Account, AccountBalances, Notification, $log, $modal) {
var vm = this;
/*
@ -38,6 +38,15 @@ module.exports = function(Account, Notification, $log, $modal) {
}
};
vm.load = function() {
vm.accounts = Account.query({}, function(result) {
return result.map(function(item) {
item.balances = AccountBalances.get({id: item.id});
return item;
});
});
};
/*
* Add an empty account.
*/
@ -58,7 +67,7 @@ module.exports = function(Account, Notification, $log, $modal) {
return account.$save().then(function(data) {
Notification.success('Account #' + data.id + ' saved.');
vm.accounts = Account.query();
vm.load();
return data;
}, function(result){
@ -100,7 +109,7 @@ module.exports = function(Account, Notification, $log, $modal) {
return account.$delete().then(function() {
Notification.success('account #' + id + ' deleted.');
vm.accounts = Account.query();
vm.load();
return account;
}, function(result) {
@ -143,5 +152,5 @@ module.exports = function(Account, Notification, $log, $modal) {
};
// Load accounts.
vm.accounts = Account.query();
vm.load();
};

View File

@ -1,26 +1,7 @@
module.exports = function($resource) {
var Account = $resource(
return $resource(
'/api/account/:id', {
id: '@id'
}
);
Account.prototype.getBalances = function() {
var Balances = $resource('/api/account/:id/balances', {id: this.id});
this.balances = Balances.get();
};
Account.prototype.getBalance = function(begin, end) {
var Balance = $resource(
'/api/account/:id/balance', {
id: this.id,
begin: begin.format('YYYY-MM-DD'),
end: end.format('YYYY-MM-DD')
});
this.balance = Balance.get();
};
return Account;
};

View File

@ -0,0 +1,7 @@
module.exports = function($resource) {
return $resource(
'/api/account/:id/balances', {
id: '@id'
}
);
};

View File

@ -39,8 +39,7 @@
<tr id="{{ account.id }}"
class="form-inline" ng-class="rowClass(account)"
ng-repeat="account in accountsCtrl.accounts | orderBy:'name'"
ng-init="account.getBalances()">
ng-repeat="account in accountsCtrl.accounts | orderBy:'name'">
<td>
<a href="#!/account/{{ account.id }}/operations">{{ account.name }}</a>
</td>

View File

@ -26,6 +26,7 @@ var ngResource = require('angular-resource'),
ngStrap = require('angular-strap');
var AccountFactory = require('./account.factory.js');
var AccountBalancesFactory = require('./accountBalances.factory.js');
var AccountConfig = require('./account.config.js');
var AccountController = require('./account.controller.js');
@ -38,6 +39,8 @@ module.exports = angular.module('accountant.accounts', [
.config(AccountConfig)
.factory('AccountBalances', AccountBalancesFactory)
.factory('Account', AccountFactory)
.controller('AccountController', AccountController)