11import { autoinject } from 'aurelia-framework' ;
2- import { LogManager , Logger } from './../../services/logging.service' ;
2+ import { ValidationControllerFactory , ValidationController , validateTrigger , ValidationRules } from 'aurelia-validation' ;
3+
4+ import { LogManager , Logger } from './../../services/logger.service' ;
35import { AppConfigService } from './../../services/app-config.service' ;
6+ import { LanguageService } from './../../services/language.service' ;
47
58@autoinject
69export class Welcome {
710 private logger : Logger ;
11+ private vController : ValidationController ;
812
913 public heading : string = 'Welcome to the Aurelia Navigation App' ;
1014 public firstName : string = 'John' ;
1115 public lastName : string = 'Doe' ;
1216 public previousValue : string = this . fullName ;
1317 public currentDate : Date = new Date ( ) ;
18+ public jsonProperty : Object = { key1 : 'value1' , key2 : 'value2' } ;
19+ public validationValid : boolean = false ;
1420
1521 constructor (
16- private appConfigService : AppConfigService
22+ private appConfigService : AppConfigService ,
23+ private languageService : LanguageService ,
24+ validationControllerFactory : ValidationControllerFactory
1725 ) {
1826 this . logger = LogManager . getLogger ( 'Welcome VM' ) ;
1927 this . logger . info ( 'appConfig => name:' , appConfigService . getName ( ) ) ;
2028 this . logger . info ( 'appConfig => version:' , appConfigService . getVersion ( ) ) ;
2129 this . logger . info ( 'appConfig => env:' , appConfigService . getEnv ( ) ) ;
2230 this . logger . info ( 'appConfig => platform:' , appConfigService . getPlatform ( ) ) ;
2331 this . logger . info ( 'appConfig => config:' , appConfigService . getConfig ( ) ) ;
32+
33+ this . vController = validationControllerFactory . createForCurrentScope ( ) ;
34+ this . vController . validateTrigger = validateTrigger . manual ;
35+ }
36+
37+ public canDeactivate ( ) : boolean {
38+ if ( this . fullName !== this . previousValue ) {
39+ return confirm ( 'Are you sure you want to leave?' ) ;
40+ }
41+ return true ;
2442 }
2543
44+ public validateFirstName ( ) : void {
45+ this . vController
46+ . validate ( {
47+ object : this ,
48+ rules : ValidationRules . ensure ( 'firstName' ) . required ( ) . rules
49+ } )
50+ . then ( r => this . validationValid = r . valid ) ;
51+ }
52+
2653 //Getters can't be directly observed, so they must be dirty checked.
2754 //However, if you tell Aurelia the dependencies, it no longer needs to dirty check the property.
2855 //To optimize by declaring the properties that this getter is computed from, uncomment the line below
@@ -37,12 +64,14 @@ export class Welcome {
3764 alert ( `Welcome, ${ this . fullName } !` ) ;
3865 }
3966
40- public canDeactivate ( ) : boolean {
41- if ( this . fullName !== this . previousValue ) {
42- return confirm ( 'Are you sure you want to leave?' ) ;
43- }
44- return true ;
45- }
67+ public switchLanguage ( ) : void {
68+ const lang = this . languageService . getCurrentLang ( ) ;
69+ if ( lang === this . languageService . getSupportedLanguages ( ) [ 0 ] ) {
70+ this . languageService . setLocale ( this . languageService . getSupportedLanguages ( ) [ 1 ] ) ;
71+ } else {
72+ this . languageService . setLocale ( this . languageService . getSupportedLanguages ( ) [ 0 ] ) ;
73+ }
74+ }
4675}
4776
4877export class UpperValueConverter {
0 commit comments