import { Http } from '@angular/http'; import { ActivatedRoute } from '@angular/router'; import { ToastsManager } from 'ng2-toastr/ng2-toastr'; import { LocaleService } from '../common/i18n'; import { SeCurisResourceServices } from '../resources/base'; import { AfterViewInit } from '@angular/core'; export interface IComboOption { id: number, label: string } export class FormBase { protected form_title: string = ''; protected form_subtitle: string = ''; protected data: any = {}; protected isNew : boolean = true; constructor(protected $L: LocaleService, protected route: ActivatedRoute, protected toaster: ToastsManager, protected resourceServices: SeCurisResourceServices, protected resourceName: string ) { } public getFieldName(fieldId: string) : string { return this.$L.get(`field.${fieldId}`); } protected loadCombos(): void {} save() { var command = this.isNew ? this.resourceServices.create(this.data) : this.resourceServices.modify(this.data.id, this.data); command.subscribe( data => this.toaster.success(this.$L.get('{} saved sucessfully', this.$L.get(this.resourceName).capitalize())), err => { console.error(err); this.toaster.success(this.$L.get('Error saving {}', this.$L.get(this.resourceName))); } ); } protected prepareData(idparam: string, default_values: any = {}) : void { this.form_title = this.$L.get('{} data', this.resourceName.capitalize()); this.isNew = true; !!this.route && this.route.params.subscribe(params => { var eleId = +params[idparam]; // (+) converts string 'id' to a number if (!eleId) { this.data = {}; Object.keys(default_values).forEach((k : string) => this.data[k] = default_values[k]); this.form_subtitle = this.$L.get('Create a new {}', this.resourceName) ; } else { this.isNew = false; this.resourceServices.get(eleId).subscribe(eleData => this.data = eleData); this.form_subtitle = this.$L.get('Modify the {} data', this.resourceName) ; } }); } }