Created
May 16, 2017 00:54
-
-
Save cristhian-net/69d4b128f712f55ca59f82ce7802c7ab to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import {ProductosRepository} from '../services/productosRepository'; | |
import {CategoriasProductoRepository} from '../services/categoriasProductoRepository'; | |
import {MateriaPrimaRepository} from '../services/materiaPrimaRepository'; | |
import { | |
ValidationControllerFactory, | |
ValidationController, | |
ValidationRules, | |
} from 'aurelia-validation'; | |
import { autoinject, computedFrom } from 'aurelia-framework'; | |
import { AureliaUXFormRenderer } from '../components/forms-form-renderer'; | |
import {Router} from 'aurelia-router'; | |
import * as toastr from 'toastr'; | |
@autoinject() | |
export class AgregarProducto { | |
public router: Router; | |
public categorias: any; | |
public heading: string; | |
public producto: Producto; | |
public controller: any; | |
public materiaPrima: any; | |
public esProduccion: boolean = false; | |
// @computedFrom('producto.CategoriaId') | |
// get esProduccion() { | |
// if (!this.categorias || this.categorias.length === 0 || !this.producto.CategoriaId) { | |
// return false; | |
// } | |
// let categoria = this.categorias.find((cat) => cat.CategoriaProductoId === this.producto.CategoriaId); | |
// if (!categoria || categoria.Nombre !== 'PRODUCCION') { | |
// return false; | |
// } | |
// return true; | |
// } | |
constructor( | |
private productosRepository: ProductosRepository, | |
private categoriasProdRepository: CategoriasProductoRepository, | |
private materiaPrimaRepository: MateriaPrimaRepository, | |
private controllerFactory: ValidationControllerFactory) { | |
this.controller = controllerFactory.createForCurrentScope(); | |
this.controller.addRenderer(new AureliaUXFormRenderer()); | |
this.heading = 'Agregar producto'; | |
this.producto = new Producto(); | |
this.controller.addObject(this); | |
this.controller.addObject(this.producto); | |
} | |
public activate(params, routeConfig, navigationInstruction) { | |
debugger; | |
this.router = navigationInstruction.router; | |
this.materiaPrimaRepository.getAll() | |
.then( | |
(materiaPrima) => { this.materiaPrima = materiaPrima; }, | |
(err) => toastr.error(err) | |
); | |
return this.categoriasProdRepository.getAll() | |
.then(categorias => { | |
this.categorias = categorias; | |
}) | |
.catch(err => { | |
toastr.error(err); | |
}); | |
} | |
public checkCategoriaById() { | |
// if (!this.categorias || this.categorias.length === 0 || !this.producto.CategoriaId) { | |
// this.esProduccion = false; | |
// } | |
// let categoria = this.categorias.find((cat) => cat.CategoriaProductoId === this.producto.CategoriaId); | |
// if (!categoria || categoria.Nombre !== 'PRODUCCION') { | |
// this.esProduccion = false; | |
// } | |
this.esProduccion = true; | |
} | |
public agregarProducto() { | |
this.controller.validate().then((value) => { | |
if (value.valid) { | |
this.productosRepository.agregarProducto(this.producto) | |
.then(producto => { | |
this.router.navigateToRoute('lista-productos'); | |
}) | |
.catch(err => toastr.error(err)); | |
} | |
}); | |
} | |
} | |
export class Producto { | |
public ProductoId: number; | |
public CategoriaId: number; | |
public Nombre: string; | |
public MateriaPrimaId: number; | |
public Precio: number; | |
constructor() { | |
this.ProductoId = 0; | |
this.Nombre = ''; | |
} | |
} | |
ValidationRules | |
.ensure((a: Producto) => a.Nombre).required().minLength(3) | |
.ensure((a: Producto) => a.CategoriaId).required() | |
.on(Producto); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment