Skip to content

Commit 67ef5e9

Browse files
committed
Refactoring productService
1 parent 7244ff4 commit 67ef5e9

File tree

4 files changed

+21
-37
lines changed

4 files changed

+21
-37
lines changed

APM - Start/app/app.component.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
import { Component } from 'angular2/core';
22
import { ProductListComponent } from './products/product-list.component';
33
import { ProductService} from './products/product.service';
4-
4+
import { HTTP_PROVIDERS } from 'angular2/http';
5+
import 'rxjs/Rx';
56

67

78
@Component({
89
selector: 'pm-app',
910
template: '<div><h1>{{pageTitle}}</h1><pm-products></pm-products></div>',
1011
directives: [ProductListComponent],
11-
providers: [ProductService]
12+
providers: [ProductService, HTTP_PROVIDERS]
1213
})
1314
export class AppComponent {
1415
pageTitle: string = "Acme Product Managment";

APM - Start/app/products/product-list.component.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import { ProductFilterPipe } from './product-filter.pipe';
44
import { StarComponent } from '../shared/star.component';
55
import { ProductService } from './product.service';
66

7-
87
@Component({
98
selector: 'pm-products',
109
templateUrl: 'app/products/product-list.component.html',
@@ -17,8 +16,9 @@ export class ProductListComponent implements OnInit{
1716
imageWidth: number = 50;
1817
imageMargin: number = 2;
1918
showImage: boolean = false;
20-
listFilter: string = 'cart';
19+
listFilter: string;
2120
products: IProduct[];
21+
errorMessage: string = "There was an error in the request."
2222

2323
constructor(private _productService: ProductService){}
2424

@@ -27,6 +27,9 @@ export class ProductListComponent implements OnInit{
2727
}
2828

2929
ngOnInit(): void{
30-
this.products = this._productService.getProducts();
30+
this._productService.getProducts()
31+
.subscribe(
32+
products => this.products = products,
33+
error => this.errorMessage = <any>error);
3134
}
3235
}
Lines changed: 10 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,17 @@
11
import { Injectable } from 'angular2/core';
22
import { IProduct } from './product';
3+
import { Http, Response } from 'angular2/http';
4+
import { Observable } from 'rxjs/observable';
5+
36

47
@Injectable()
58
export class ProductService {
6-
getProducts(): IProduct[] {
7-
return [
8-
{
9-
"productId": 1,
10-
"productName": "Leaf Rake",
11-
"productCode": "GDN-0011",
12-
"releaseDate": "March 19, 2016",
13-
"description": "Leaf rake with 48-inch wooden handle.",
14-
"price": 19.95,
15-
"starRating": 3.2,
16-
"imageUrl": "http://openclipart.org/image/300px/svg_to_png/26215/Anonymous_Leaf_Rake.png"
17-
},
18-
{
19-
"productId": 2,
20-
"productName": "Garden Cart",
21-
"productCode": "GDN-0023",
22-
"releaseDate": "March 18, 2016",
23-
"description": "15 gallon capacity rolling garden cart",
24-
"price": 32.99,
25-
"starRating": 4.2,
26-
"imageUrl": "http://openclipart.org/image/300px/svg_to_png/58471/garden_cart.png"
27-
},
28-
{
29-
"productId": 5,
30-
"productName": "Hammer",
31-
"productCode": "TBX-0048",
32-
"releaseDate": "May 21, 2016",
33-
"description": "Curved claw steel hammer",
34-
"price": 8.9,
35-
"starRating": 4.8,
36-
"imageUrl": "http://openclipart.org/image/300px/svg_to_png/73/rejon_Hammer.png"
37-
}];
9+
private _productUrl = 'api/products/products.json';
10+
11+
constructor(private _http: Http){}
12+
13+
getProducts(): Observable<IProduct[]> {
14+
return this._http.get(this._productUrl)
15+
.map((response: Response) => <IProduct[]>response.json());
3816
}
3917
}

APM - Start/index.html

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
<script src="node_modules/rxjs/bundles/Rx.js"></script>
2020
<script src="node_modules/angular2/bundles/angular2.dev.js"></script>
2121

22+
<script src="node_modules/angular2/bundles/http.dev.js"></script>
23+
2224
<!-- 2. Configure SystemJS -->
2325
<script>
2426
System.config({

0 commit comments

Comments
 (0)