diff --git a/src/accounts/account.controller.js b/src/accounts/account.controller.js index 9ef9e2d..73ea57c 100644 --- a/src/accounts/account.controller.js +++ b/src/accounts/account.controller.js @@ -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(); }; diff --git a/src/accounts/account.factory.js b/src/accounts/account.factory.js index e604e97..1f24176 100644 --- a/src/accounts/account.factory.js +++ b/src/accounts/account.factory.js @@ -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; }; diff --git a/src/accounts/accountBalances.factory.js b/src/accounts/accountBalances.factory.js new file mode 100644 index 0000000..df19920 --- /dev/null +++ b/src/accounts/accountBalances.factory.js @@ -0,0 +1,7 @@ +module.exports = function($resource) { + return $resource( + '/api/account/:id/balances', { + id: '@id' + } + ); +}; diff --git a/src/accounts/accounts.html b/src/accounts/accounts.html index 86d3db5..0916589 100644 --- a/src/accounts/accounts.html +++ b/src/accounts/accounts.html @@ -39,8 +39,7 @@ + ng-repeat="account in accountsCtrl.accounts | orderBy:'name'"> {{ account.name }} diff --git a/src/accounts/index.js b/src/accounts/index.js index 4ae9dce..75e3bc0 100644 --- a/src/accounts/index.js +++ b/src/accounts/index.js @@ -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)