From 9f9fd79047b6aafeb58948c488c301533e135d6f Mon Sep 17 00:00:00 2001 From: Chris Thielen Date: Sat, 10 Feb 2018 15:04:12 -0800 Subject: [PATCH] Demonstrate app_initializer --- src/app/app.module.ts | 31 +++++++++++++++++++++++++++++-- src/app/router.config.ts | 1 + 2 files changed, 30 insertions(+), 2 deletions(-) diff --git a/src/app/app.module.ts b/src/app/app.module.ts index 50b299a6..4395ca79 100644 --- a/src/app/app.module.ts +++ b/src/app/app.module.ts @@ -1,7 +1,8 @@ import { BrowserModule } from '@angular/platform-browser'; -import { NgModule, NgModuleFactoryLoader, SystemJsNgModuleLoader } from '@angular/core'; +import { APP_INITIALIZER, NgModule, NgModuleFactoryLoader, SystemJsNgModuleLoader } from '@angular/core'; import { FormsModule } from '@angular/forms'; import { HttpModule } from '@angular/http'; +import { UIRouter } from '@uirouter/core'; import { AppComponent } from './app.component'; import { WelcomeComponent } from './welcome.component'; @@ -12,6 +13,25 @@ import { APP_STATES } from './app.states'; import { GlobalModule } from './global/global.module'; import { routerConfigFn } from './router.config'; +export class AppConfig { + load() { + const timeout = 4; + return new Promise(resolve => setTimeout(resolve, 4 * 1000)) + .then(() => console.log(`loaded after ${timeout} seconds!`)); + } +} + +export function initResources(config: AppConfig, uiRouter: UIRouter) { + const router = uiRouter; + + return function () { + config.load().then(function () { + router.urlService.listen(); + router.urlService.sync(); + }); + } +} + @NgModule({ declarations: [ AppComponent, @@ -32,7 +52,14 @@ import { routerConfigFn } from './router.config'; HttpModule ], providers: [ - { provide: NgModuleFactoryLoader, useClass: SystemJsNgModuleLoader } + AppConfig, + { provide: NgModuleFactoryLoader, useClass: SystemJsNgModuleLoader }, + { + provide: APP_INITIALIZER, + useFactory: initResources, + deps: [AppConfig, UIRouter], + multi: true, + }, ], bootstrap: [UIView] }) diff --git a/src/app/router.config.ts b/src/app/router.config.ts index 50806e2d..08056505 100644 --- a/src/app/router.config.ts +++ b/src/app/router.config.ts @@ -9,6 +9,7 @@ export function routerConfigFn(router: UIRouter) { requiresAuthHook(transitionService); googleAnalyticsHook(transitionService); + router.urlService.deferIntercept(); router.trace.enable(Category.TRANSITION); router.plugin(Visualizer); }