Skip to content

Commit c5e2cc1

Browse files
committed
split 10 into 10 and 11
1 parent d846d0e commit c5e2cc1

23 files changed

+377
-117
lines changed

app/app.component.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import { AppComponent as C09Component } from './chapter-09/app.component';
2222
import { AppComponent as C09ecComponent } from './chapter-09-exercise-completed/app.component';
2323
import { AppComponent as C10Component } from './chapter-10/app.component';
2424
import { AppComponent as C11Component } from './chapter-11/app.component';
25+
import { AppComponent as C12Component } from './chapter-12/app.component';
2526
import { AppComponent as C98Component } from './chapter-98/app.component';
2627
import { AppComponent as C99Component } from './chapter-99/app.component';
2728

@@ -52,6 +53,7 @@ const chapters: { [index: string]: { component: any, routes: Routes } } = {
5253
'Chapter 9: exercise (completed)': { component: C09ecComponent, routes: noRoutes },
5354
'Chapter 10': { component: C10Component, routes: noRoutes },
5455
'Chapter 11': { component: C11Component, routes: noRoutes },
56+
'Chapter 12': { component: C12Component, routes: noRoutes },
5557

5658
'Chapter 98': { component: C98Component, routes: c98Routes },
5759
'Chapter 99': { component: C99Component, routes: c99Routes },

app/app.module.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,10 @@ import { AppModule as C09Module } from './chapter-09/app.module';
2121
import { AppModule as C09ecModule } from './chapter-09-exercise-completed/app.module';
2222
import { AppModule as C10Module } from './chapter-10/app.module';
2323
import { AppModule as C11Module } from './chapter-11/app.module';
24-
import { AppModule as C98Module } from './chapter-98/app.module';
24+
import { AppModule as C12Module } from './chapter-12/app.module';
25+
26+
27+
import { AppModule as C98Module } from './chapter-98/app.module';
2528
import { AppModule as C99Module } from './chapter-99/app.module';
2629

2730
import { AppComponent, ChapterViewDirective } from './app.component';
@@ -46,6 +49,7 @@ import { InMemoryDataService } from './in-memory-data.service';
4649
C09Module, C09ecModule,
4750
C10Module,
4851
C11Module,
52+
C12Module,
4953
C98Module,
5054
C99Module,
5155

app/chapter-10/customer-list.component.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,7 @@ export class CustomerListComponent implements OnInit {
2727
this.isBusy = true;
2828
this.logger.log('Getting customers ...');
2929

30-
// this.dataService.getCustomersP().then( // promise version
31-
this.dataService.getCustomers().subscribe( // observable version
30+
this.dataService.getCustomers().then( // promise version
3231
custs => {
3332
this.isBusy = false;
3433
this.customers = custs;

app/chapter-10/data.service.ts

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,13 @@ import { createTestCustomers } from '../test-data';
66

77
import { LoggerService } from './logger.service';
88

9-
import { Observable } from 'rxjs/Observable';
10-
import { of } from 'rxjs/observable/of';
11-
import 'rxjs/add/operator/do';
12-
import 'rxjs/add/operator/delay';
13-
149
@Injectable()
1510
export class DataService {
1611

1712
constructor(private logger: LoggerService) { }
1813

1914
/** Get existing customers as a Promise */
20-
getCustomersP(): Promise<Customer[]> {
15+
getCustomers(): Promise<Customer[]> {
2116
this.logger.log('Getting customers as a Promise ...');
2217

2318
const customers = createTestCustomers();
@@ -29,15 +24,4 @@ export class DataService {
2924
}, 1500); // simulate server response latency
3025
});
3126
}
32-
33-
/** Get existing customers as an Observable */
34-
getCustomers(): Observable<Customer[]> {
35-
this.logger.log('Getting customers as an Observable ...');
36-
37-
const customers = createTestCustomers();
38-
39-
return of(customers)
40-
.delay(1500) // simulate server response latency
41-
.do(custs => this.logger.log(`Got ${custs.length} customers`));
42-
}
4327
}

app/chapter-11/app.module.ts

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import { NgModule } from '@angular/core';
22
import { BrowserModule } from '@angular/platform-browser';
33
import { FormsModule } from '@angular/forms';
4-
import { HttpModule } from '@angular/http'; // <-- import HttpModule
54

65
import { AppComponent } from './app.component';
76
import { AddressComponent } from './address.component';
@@ -11,18 +10,10 @@ import { CustomerListComponent } from './customer-list.component';
1110
import { DataService } from './data.service';
1211
import { LoggerService } from './logger.service';
1312

14-
// in-mem-web-api and its test-data service
15-
import { InMemoryWebApiModule } from 'angular-in-memory-web-api';
16-
import { InMemoryDataService } from './in-memory-data.service';
17-
1813
@NgModule({
1914
imports: [
2015
BrowserModule,
21-
FormsModule,
22-
23-
HttpModule, // <-- add HttpModule here
24-
25-
InMemoryWebApiModule.forRoot(InMemoryDataService) // <-- register in-mem-web-api and its data
16+
FormsModule
2617
],
2718
declarations: [
2819
AppComponent,

app/chapter-11/customer-detail.component.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,6 @@ export class CustomerDetailComponent {
1717

1818
hideAddress = false;
1919

20-
left() { this.shift.emit(-1); }
21-
right() { this.shift.emit(1); }
20+
left() { this.shift.emit(-1); }
21+
right() { this.shift.emit(1); }
2222
}
Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,6 @@
11
<h1>Customers</h1>
22
<p *ngIf="isBusy"><i>Loading customers ...</i></p>
3-
<p *ngIf="!isBusy">
4-
<i>Click to select a customer</i> &nbsp;
5-
<button (click)="getCustomers()">Refresh</button> &nbsp;
6-
<button (click)="save(customer)" [hidden]="!customer">Save</button> &nbsp;
7-
</p>
3+
<p *ngIf="!isBusy"><i>Click to select a customer</i></p>
84

95
<ul>
106
<li *ngFor="let cust of customers"
@@ -16,8 +12,5 @@ <h1>Customers</h1>
1612

1713
<div *ngIf="customer">
1814
<hr>
19-
<customer-detail [customer]="customer"
20-
(shift)="shift($event)"
21-
(save)="save($event)">
22-
</customer-detail>
15+
<customer-detail [customer]="customer" (shift)="shift($event)"></customer-detail>
2316
</div>

app/chapter-11/customer-list.component.ts

Lines changed: 6 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -24,34 +24,15 @@ export class CustomerListComponent implements OnInit {
2424
ngOnInit() { this.getCustomers(); }
2525

2626
getCustomers() {
27-
this.customer = undefined; // <-- clear before refresh
28-
this.customers = undefined;
29-
3027
this.isBusy = true;
3128
this.logger.log('Getting customers ...');
3229

33-
// this.dataService.getCustomersP().then( // Promise version
34-
this.dataService.getCustomers().subscribe( // Observable version
35-
custs => {
36-
this.isBusy = false;
37-
this.customers = custs;
38-
},
39-
(errorMsg: string) => {
40-
this.isBusy = false;
41-
alert(errorMsg);
42-
}
43-
);
44-
}
45-
46-
save(customer: Customer) {
47-
if (!customer) { return; }
48-
this.isBusy = true;
49-
this.logger.log('Saving ...');
50-
this.dataService.update(customer)
51-
.subscribe(
52-
() => this.isBusy = false,
53-
() => this.isBusy = false
54-
);
30+
// this.dataService.getCustomersP().then( // promise version
31+
this.dataService.getCustomers().subscribe( // observable version
32+
custs => {
33+
this.isBusy = false;
34+
this.customers = custs;
35+
});
5536
}
5637

5738
shift(increment: number) {

app/chapter-11/data.service.ts

Lines changed: 19 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,75 +1,43 @@
11
// Observable DataService
2-
import { Injectable } from '@angular/core';
3-
import { Headers, Http } from '@angular/http'; // <-- import Http & Headers
2+
import { Injectable } from '@angular/core';
3+
4+
import { Customer } from './model';
5+
import { createTestCustomers } from '../test-data';
46

5-
import { Customer } from './model';
67
import { LoggerService } from './logger.service';
78

89
import { Observable } from 'rxjs/Observable';
9-
10-
import 'rxjs/add/operator/catch'; // <-- add rxjs operator extensions used here
10+
import { of } from 'rxjs/observable/of';
1111
import 'rxjs/add/operator/do';
12-
import 'rxjs/add/operator/map';
13-
import 'rxjs/add/operator/toPromise';
14-
15-
import 'rxjs/add/observable/throw'; // <-- add rxjs Observable extensions used here
12+
import 'rxjs/add/operator/delay';
1613

1714
@Injectable()
1815
export class DataService {
19-
private customersUrl = 'api/customers';
20-
private headers = new Headers({'Content-Type': 'application/json'});
2116

22-
constructor(
23-
private http: Http, // <-- inject http
24-
private logger: LoggerService) { }
17+
constructor(private logger: LoggerService) { }
2518

2619
/** Get existing customers as a Promise */
2720
getCustomersP(): Promise<Customer[]> {
2821
this.logger.log('Getting customers as a Promise ...');
2922

30-
return this.http.get(this.customersUrl) // <-- returns an observable
31-
.toPromise() // <-- convert immediately to a promise
32-
.then(response => {
33-
const custs = response.json().data as Customer[]; // <-- extract data from the response
34-
this.logger.log(`Got ${custs.length} customers`);
35-
return custs;
36-
})
37-
.catch((error: any) => {
38-
this.logger.log(`An error occurred ${error}`); // for demo purposes only
39-
// re-throw user-facing message
40-
return Promise.reject('Something bad happened; check the console');
41-
});
42-
}
23+
const customers = createTestCustomers();
4324

25+
return new Promise(resolve => {
26+
setTimeout(() => {
27+
this.logger.log(`Got ${customers.length} customers`);
28+
resolve(customers);
29+
}, 1500); // simulate server response latency
30+
});
31+
}
4432

4533
/** Get existing customers as an Observable */
4634
getCustomers(): Observable<Customer[]> {
4735
this.logger.log('Getting customers as an Observable ...');
4836

49-
return this.http.get(this.customersUrl)
50-
.map(response => response.json().data as Customer[]) // <-- extract data
51-
.do(custs => this.logger.log(`Got ${custs.length} customers`))
52-
.catch(error => this.handleError(error));
53-
}
54-
55-
/** Common Http Observable error handler */
56-
private handleError(error: any): Observable<any> {
57-
this.logger.log(`An error occurred ${error}`); // for demo purposes only
58-
// re-throw user-facing message
59-
return Observable.throw('Something bad happened; check the console');
60-
}
61-
62-
63-
/** Update existing customer */
64-
update(customer: Customer): Observable<any> {
65-
const url = `${this.customersUrl}/${customer.id}`;
66-
const result = this.http.put(url, customer, { headers: this.headers })
67-
// .do(() => this.logger.log(`Saved customer ${customer.name}`))
68-
.catch(error => this.handleError(error));
69-
70-
// Result is "cold". Ensure logging even if caller doesn't subscribe
71-
result.subscribe(() => this.logger.log(`Saved customer ${customer.name}`));
37+
const customers = createTestCustomers();
7238

73-
return result;
39+
return of(customers)
40+
.delay(1500) // simulate server response latency
41+
.do(custs => this.logger.log(`Got ${custs.length} customers`));
7442
}
7543
}

app/chapter-12/address.component.html

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<div *ngIf="address">
2+
<!-- Diagnostic {{ address | json }}-->
3+
<fieldset>
4+
<label>
5+
Street:
6+
<input [(ngModel)]="address.street" placeholder="Street">
7+
</label>
8+
</fieldset>
9+
<fieldset>
10+
<label>
11+
City:
12+
<input [(ngModel)]="address.city" placeholder="City">
13+
</label>
14+
</fieldset>
15+
<fieldset>
16+
<label>
17+
State:
18+
<select [(ngModel)]="address.state">
19+
<option *ngFor="let state of states">{{state}}</option>
20+
</select>
21+
</label>
22+
</fieldset>
23+
\ <fieldset>
24+
<label>
25+
Region:
26+
<select [(ngModel)]="address.region">
27+
<option *ngFor="let region of regions">{{region}}</option>
28+
</select>
29+
</label>
30+
</fieldset>
31+
32+
</div>

0 commit comments

Comments
 (0)