rsanchez
2017-03-19 280daa7f3f858ecfef9c91ffd5dea1007f021048
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
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) ;
           }
       });
   }
}