// Copyright (c) 2016, hackbard. All rights reserved. Use of this source code // is governed by a BSD-style license that can be found in the LICENSE file. import 'dart:async'; import 'package:angular2/core.dart'; import 'package:angular2/router.dart'; import 'product.dart'; import 'product_service.dart'; import 'product_category.dart'; import 'product_category_service.dart'; import 'package:angular2_rbi/directives.dart'; @Component( selector: 'my-products', templateUrl: 'product_component.html', styleUrls: const ['product_component.css'], directives: const [MaterialTextfield,MaterialButton], providers: const [ProductService,ProductCategoryService] ) class ProductComponent implements OnInit { final ProductService _prodSrv; final ProductCategoryService _prodcatSrv; final RouteParams _routeParams; final Router _router; String catid; String prod_category_name; String prod_name; String prod_price; int prodcnt; List products; ProductCategory prodcat; ProductComponent(this._prodSrv,this._prodcatSrv, this._routeParams,this._router) { prod_category_name='Category'; } Future ngOnInit() async { catid=_routeParams.get('id'); if(catid!=null) { products = await (_prodSrv.getByCategory(catid)); prodcat = await (_prodcatSrv.getById(catid)); } prod_category_name=prodcat.name; prodcnt=products.length; } Future updateProductCategory() async { if(prodcat.name!=prod_category_name) { await _prodcatSrv.updateProdCategory( catid, prod_category_name ); _router.navigate(['ProductCategories']); } } Future deleteProductCategory() async { print('Debug: Deleting product category '+ prodcat.name+'/'+prodcat.id); await _prodcatSrv.deleteProdCategory(prodcat.id); _router.navigate(['ProductCategories']); } Future createProduct() async { if(prod_name==null || prod_price==null) return; print('Debug: Creating product '+prod_name+'/'+ prod_price); String id = await _prodSrv.createProduct(prod_name, double.parse(prod_price), catid); // instead of executing ngOnInit to 'reload' content products.add(new Product( id,prod_name,double.parse(prod_price),'product',catid )); prod_name=''; prod_price=''; } choose(Product prod) { goto_product(prod); } Future goto_product(Product prod) => _router.navigate([ 'ProductDetail', {'id': prod.id.toString()} ]); }