From de945cd16c514248eb9203b6cbb35e71d04a0cef Mon Sep 17 00:00:00 2001 From: Alexis Lahouze Date: Sun, 16 Jul 2017 17:03:37 +0200 Subject: [PATCH] Create service for AccountBalances and use it. --- src/accounts/account.component.ts | 19 ++++++++++++++++--- src/accounts/account.module.ts | 6 ++++-- src/accounts/accountBalances.service.ts | 18 ++++++++++++++++++ src/accounts/index.ts | 4 ++-- 4 files changed, 40 insertions(+), 7 deletions(-) create mode 100644 src/accounts/accountBalances.service.ts diff --git a/src/accounts/account.component.ts b/src/accounts/account.component.ts index c01cef8..841799a 100644 --- a/src/accounts/account.component.ts +++ b/src/accounts/account.component.ts @@ -4,13 +4,22 @@ var accountFormTmpl = require('./account.form.tmpl.html'), accountDeleteTmpl = require('./account.delete.tmpl.html'); import { Account } from './account'; +import { AccountBalances } from './accountBalances'; +import { AccountService } from './account.service'; +import { AccountBalancesService } from './accountBalances.service'; export class AccountComponent { - static $inject = ['AccountService', 'AccountBalances', 'Notification', '$log', '$modal']; + static $inject = ['AccountService', 'AccountBalancesService', 'Notification', '$log', '$modal']; accounts: Account[]; - constructor(private AccountService, private AccountBalances, private Notification, private $log, private $modal) { + constructor( + private AccountService: AccountService, + private AccountBalancesService: AccountBalancesService, + private Notification, + private $log, + private $modal + ) { // Load accounts. this.load(); } @@ -52,7 +61,11 @@ export class AccountComponent { load() { this.AccountService.query().subscribe(accounts => { this.accounts = accounts.map((account: Account) => { - account.balances = this.AccountBalances.get({id: account.id}); + this.AccountBalancesService + .get(account.id) + .subscribe((accountBalances: AccountBalances) => { + account.balances = accountBalances; + }) return account; }) }); diff --git a/src/accounts/account.module.ts b/src/accounts/account.module.ts index a684b94..ee89a7c 100644 --- a/src/accounts/account.module.ts +++ b/src/accounts/account.module.ts @@ -7,6 +7,7 @@ import { NgLoggerModule, Level } from '@nsalaun/ng-logger'; import { RestangularModule } from 'ngx-restangular'; import { AccountService } from './account.service'; +import { AccountBalancesService } from './accountBalances.service'; @NgModule({ imports: [ @@ -15,8 +16,9 @@ import { AccountService } from './account.service'; RestangularModule ], providers: [ - AccountService - ] + AccountService, + AccountBalancesService, + ], }) export class AccountModule {} diff --git a/src/accounts/accountBalances.service.ts b/src/accounts/accountBalances.service.ts new file mode 100644 index 0000000..77b99fa --- /dev/null +++ b/src/accounts/accountBalances.service.ts @@ -0,0 +1,18 @@ +// vim: set tw=80 ts=2 sw=2 sts=2 : +import { Injectable } from '@angular/core'; +import { Observable } from 'rxjs/Rx'; + +import { Restangular } from "ngx-restangular"; + +import { AccountBalances } from './accountBalances'; + +@Injectable() +export class AccountBalancesService { + constructor( + private restangular: Restangular + ) {} + + get(id: number): Observable { + return this.restangular.one('account', id).one('balances').get(); + } +} diff --git a/src/accounts/index.ts b/src/accounts/index.ts index 5d461a5..e4a3e6f 100644 --- a/src/accounts/index.ts +++ b/src/accounts/index.ts @@ -29,7 +29,7 @@ var ngResource = require('angular-resource'), var accountsTmpl = require('./accounts.html'); -import { AccountBalancesFactory } from './accountBalances.factory'; +import { AccountBalancesService } from './accountBalances.service'; import { AccountService } from './account.service'; import { AccountComponent } from './account.component'; @@ -40,7 +40,7 @@ export default angular.module('accountant.accounts', [ ngStrap, ]) - .factory('AccountBalances', AccountBalancesFactory) + .factory('AccountBalancesService', downgradeInjectable(AccountBalancesService)) .factory('AccountService', downgradeInjectable(AccountService))