1 // Copyright (c) 2016, hackbard. All rights reserved. Use of this source code
2 // is governed by a BSD-style license that can be found in the LICENSE file.
6 import 'package:angular2/core.dart';
7 import 'package:angular2/router.dart';
10 import 'product_service.dart';
11 import 'product_category.dart';
12 import 'product_category_service.dart';
14 import 'package:angular2_rbi/directives.dart';
17 selector: 'my-products',
18 templateUrl: 'product_component.html',
19 styleUrls: const ['product_component.css'],
20 directives: const [MaterialTextfield,MaterialButton],
21 providers: const [ProductService,ProductCategoryService]
24 class ProductComponent implements OnInit {
25 final ProductService _prodSrv;
26 final ProductCategoryService _prodcatSrv;
27 final RouteParams _routeParams;
31 String prod_category_name;
37 List<Product> products;
38 ProductCategory prodcat;
40 ProductComponent(this._prodSrv,this._prodcatSrv,
41 this._routeParams,this._router) {
42 prod_category_name='Category';
45 Future<Null> ngOnInit() async {
46 catid=_routeParams.get('id');
48 products = await (_prodSrv.getByCategory(catid));
49 prodcat = await (_prodcatSrv.getById(catid));
51 prod_category_name=prodcat.name;
52 prodcnt=products.length;
55 Future<Null> updateProductCategory() async {
56 if(prodcat.name!=prod_category_name) {
57 await _prodcatSrv.updateProdCategory(
61 _router.navigate(['ProductCategories']);
65 Future<Null> deleteProductCategory() async {
66 print('Debug: Deleting product category '+
67 prodcat.name+'/'+prodcat.id);
68 await _prodcatSrv.deleteProdCategory(prodcat.id);
69 _router.navigate(['ProductCategories']);
72 Future<Null> createProduct() async {
73 if(prod_name==null || prod_price==null)
75 print('Debug: Creating product '+prod_name+'/'+
76 prod_price.toString());
77 String id = await _prodSrv.createProduct(prod_name,
78 double.parse(prod_price),
80 // instead of executing ngOnInit to 'reload' content
81 products.add(new Product(
82 id,prod_name,double.parse(prod_price),'product',catid
88 choose(Product prod) {
92 Future<Null> goto_product(Product prod) => _router.navigate([
94 {'id': prod.id.toString()}