@@ -3,7 +3,7 @@ import { BrowserModule } from '@angular/platform-browser';
33import { FormsModule } from '@angular/forms' ;
44import { HttpModule } from '@angular/http' ;
55import { RouterModule } from '@angular/router' ;
6- import { removeNgStyles , createNewHosts } from '@angularclass/hmr' ;
6+ import { removeNgStyles , createNewHosts , createInputTransfer } from '@angularclass/hmr' ;
77
88/*
99 * Platform and Environment providers/directives/pipes
@@ -26,8 +26,8 @@ const APP_PROVIDERS = [
2626] ;
2727
2828type StoreType = {
29- $inputs : any [ ] ,
3029 state : InteralStateType ,
30+ restoreInputValues : ( ) => void ,
3131 disposeOldHosts : ( ) => void
3232} ;
3333
@@ -59,36 +59,25 @@ export class AppModule {
5959
6060 hmrOnInit ( store : StoreType ) {
6161 if ( ! store || ! store . state ) return ;
62- console . log ( 'HMR store' , store ) ;
62+ console . log ( 'HMR store' , JSON . stringify ( store , null , 2 ) ) ;
6363 // set state
6464 this . appState . _state = store . state ;
65-
6665 // set input values
67- const inputs = document . querySelectorAll ( 'input' ) ;
68- if ( store . $inputs && inputs . length === store . $inputs . length ) {
69- store . $inputs . forEach ( ( value , i ) => {
70- let el = inputs [ i ] ;
71- el . value = value ;
72- el . dispatchEvent ( new CustomEvent ( 'input' , { detail : el . value } ) ) ;
73- } ) ;
74- }
75-
66+ if ( 'restoreInputValues' in store ) { store . restoreInputValues ( ) ; }
7667 this . appRef . tick ( ) ;
7768 delete store . state ;
69+ delete store . restoreInputValues ;
7870 }
7971
8072 hmrOnDestroy ( store : StoreType ) {
8173 const cmpLocation = this . appRef . components . map ( cmp => cmp . location . nativeElement ) ;
82- // recreate elements
8374 // save state
8475 const state = this . appState . _state ;
8576 store . state = state ;
77+ // recreate root elements
8678 store . disposeOldHosts = createNewHosts ( cmpLocation ) ;
87-
8879 // save input values
89- const inputs = document . querySelectorAll ( 'input' ) ;
90- const $inputs = [ ] . slice . call ( inputs ) . map ( input => input . value ) ;
91- store . $inputs = $inputs ;
80+ store . restoreInputValues = createInputTransfer ( ) ;
9281 // remove styles
9382 removeNgStyles ( ) ;
9483 }
0 commit comments