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'), var accountFormTmpl = require('./account.form.tmpl.html'),
accountDeleteTmpl = require('./account.delete.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; 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. * Add an empty account.
*/ */
@ -58,7 +67,7 @@ module.exports = function(Account, Notification, $log, $modal) {
return account.$save().then(function(data) { return account.$save().then(function(data) {
Notification.success('Account #' + data.id + ' saved.'); Notification.success('Account #' + data.id + ' saved.');
vm.accounts = Account.query(); vm.load();
return data; return data;
}, function(result){ }, function(result){
@ -100,7 +109,7 @@ module.exports = function(Account, Notification, $log, $modal) {
return account.$delete().then(function() { return account.$delete().then(function() {
Notification.success('account #' + id + ' deleted.'); Notification.success('account #' + id + ' deleted.');
vm.accounts = Account.query(); vm.load();
return account; return account;
}, function(result) { }, function(result) {
@ -143,5 +152,5 @@ module.exports = function(Account, Notification, $log, $modal) {
}; };
// Load accounts. // Load accounts.
vm.accounts = Account.query(); vm.load();
}; };

View File

@ -1,26 +1,7 @@
module.exports = function($resource) { module.exports = function($resource) {
var Account = $resource( return $resource(
'/api/account/:id', { '/api/account/:id', {
id: '@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 }}" <tr id="{{ account.id }}"
class="form-inline" ng-class="rowClass(account)" class="form-inline" ng-class="rowClass(account)"
ng-repeat="account in accountsCtrl.accounts | orderBy:'name'" ng-repeat="account in accountsCtrl.accounts | orderBy:'name'">
ng-init="account.getBalances()">
<td> <td>
<a href="#!/account/{{ account.id }}/operations">{{ account.name }}</a> <a href="#!/account/{{ account.id }}/operations">{{ account.name }}</a>
</td> </td>

View File

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