11import { autoinject } from 'aurelia-framework' ;
2+ import { ValidationControllerFactory , ValidationController , validateTrigger , ValidationRules } from 'aurelia-validation' ;
23
34import { LogManager , Logger } from './../../services/logger.service' ;
45import { AppConfigService } from './../../services/app-config.service' ;
@@ -7,25 +8,48 @@ import { LanguageService } from './../../services/language.service';
78@autoinject
89export class Welcome {
910 private logger : Logger ;
11+ private vController : ValidationController ;
1012
1113 public heading : string = 'Welcome to the Aurelia Navigation App' ;
1214 public firstName : string = 'John' ;
1315 public lastName : string = 'Doe' ;
1416 public previousValue : string = this . fullName ;
1517 public currentDate : Date = new Date ( ) ;
18+ public jsonProperty : Object = { key1 : 'value1' , key2 : 'value2' } ;
19+ public validationValid : boolean = false ;
1620
1721 constructor (
1822 private appConfigService : AppConfigService ,
19- private languageService : LanguageService
23+ private languageService : LanguageService ,
24+ validationControllerFactory : ValidationControllerFactory
2025 ) {
2126 this . logger = LogManager . getLogger ( 'Welcome VM' ) ;
2227 this . logger . info ( 'appConfig => name:' , appConfigService . getName ( ) ) ;
2328 this . logger . info ( 'appConfig => version:' , appConfigService . getVersion ( ) ) ;
2429 this . logger . info ( 'appConfig => env:' , appConfigService . getEnv ( ) ) ;
2530 this . logger . info ( 'appConfig => platform:' , appConfigService . getPlatform ( ) ) ;
2631 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 ;
2742 }
2843
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+
2953 //Getters can't be directly observed, so they must be dirty checked.
3054 //However, if you tell Aurelia the dependencies, it no longer needs to dirty check the property.
3155 //To optimize by declaring the properties that this getter is computed from, uncomment the line below
@@ -40,13 +64,6 @@ export class Welcome {
4064 alert ( `Welcome, ${ this . fullName } !` ) ;
4165 }
4266
43- public canDeactivate ( ) : boolean {
44- if ( this . fullName !== this . previousValue ) {
45- return confirm ( 'Are you sure you want to leave?' ) ;
46- }
47- return true ;
48- }
49-
5067 public switchLanguage ( ) : void {
5168 const lang = this . languageService . getCurrentLang ( ) ;
5269 if ( lang === this . languageService . getSupportedLanguages ( ) [ 0 ] ) {
0 commit comments