4
4
// Angular 2
5
5
import { Component , View , coreDirectives , onChange , ON_PUSH } from 'angular2/angular2' ;
6
6
import { Observable } from 'angular2/src/facade/async' ;
7
+ import { Inject } from 'angular2/di'
7
8
import { RouteConfig , Router , RouterOutlet , RouterLink } from 'angular2/router' ;
8
9
var routerDirectives = [ RouterOutlet , RouterLink ] ;
9
10
import { BrowserLocation } from 'angular2/src/router/browser_location' ;
@@ -26,9 +27,7 @@ import {GreetModel} from '../models/GreetModel';
26
27
selector : 'count' ,
27
28
lifecycle : [ onChange ] ,
28
29
changeDetection : ON_PUSH ,
29
- properties : {
30
- 'counter' : 'counter'
31
- }
30
+ properties : { 'counter' : 'counter' }
32
31
} )
33
32
@View ( {
34
33
template : `
@@ -46,25 +45,27 @@ import {GreetModel} from '../models/GreetModel';
46
45
`
47
46
} )
48
47
export class Count {
49
- counter : number ;
48
+ counter : number = 0 ;
50
49
counterIntent : CounterIntent ;
51
50
constructor ( counterIntent : CounterIntent ) {
52
51
this . counterIntent = counterIntent ;
52
+ // console.log('counter', this.counter)
53
53
}
54
54
55
- onChange ( value ) {
56
- console . log ( 'CHANGE Count\n' , JSON . stringify ( value , null , 2 ) ) ;
57
- }
58
55
incrementCounter ( ) {
59
56
this . counterIntent . incrementCounter ( ) ;
60
57
}
58
+
59
+ onChange ( value ) {
60
+ console . log ( 'CHANGE Count' , '\n' , JSON . stringify ( value , null , 2 ) ) ;
61
+ }
61
62
}
62
63
63
64
64
- // App: Top Level Component
65
+
65
66
@Component ( {
66
- selector : 'app' , // without [ ] means we are selecting the tag directly,
67
- lifecycle : [ onChange ]
67
+ selector : 'app' ,
68
+ lifecycle : [ onChange ] ,
68
69
// changeDetection: ON_PUSH
69
70
} )
70
71
@View ( {
@@ -74,56 +75,47 @@ export class Count {
74
75
75
76
<h1 class="title">Hello Reactive Angular 2</h1>
76
77
78
+ <test-app></test-app>
77
79
78
- <count [counter]="state.counter">
80
+
81
+ <count [counter]="appState | async | get('counter')">
79
82
<button (click)="handleIncrement()">Increment Counter from App</button>
80
83
</count>
81
84
82
- <h2>Greet {{ state. greeting }}</h2>
85
+ <h2>Greet {{ appState | async | get(' greeting') }}</h2>
83
86
<div>
84
- <button (^click)="toggleGreet()">Greet {{ state. greeting }} </button>
87
+ <button (^click)="toggleGreet()">Greet {{ appState | async | get(' greeting') }} </button>
85
88
</div>
86
- <pre>state = {{ state | json }}</pre>
87
89
<pre>appState = {{ appState | async | json }}</pre>
88
90
`
89
91
} )
90
92
export class App {
91
93
state : any ;
92
94
appState : any ;
93
- // public isn't working for me here :/
94
- counter : CounterModel ; counterIntent : CounterIntent ;
95
- greet : GreetModel ; greetIntent : GreetIntent ;
96
- constructor ( counter : CounterModel , counterIntent : CounterIntent ,
97
- greet : GreetModel , greetIntent : GreetIntent ) {
98
- this . counter = counter ; this . counterIntent = counterIntent ;
99
- this . greet = greet ; this . greetIntent = greetIntent ;
100
-
101
- this . state = { } ;
102
-
103
- this . appState = Rx . Observable . merge (
104
- this . counter . subject . toRx ( ) ,
105
- this . greet . subject . toRx ( )
95
+ constructor (
96
+ public counter : CounterModel ,
97
+ public counterIntent : CounterIntent ,
98
+ public greet : GreetModel ,
99
+ public greetIntent : GreetIntent ,
100
+ @Inject ( 'initilAppState' ) initilAppState
101
+ ) {
102
+
103
+ this . appState = Rx . Observable . return ( initilAppState ) . combineLatest (
104
+ this . counter . subject ,
105
+ this . greet . subject ,
106
+ ( ...args ) => Object . assign ( { } , ...args )
106
107
) ;
107
108
108
-
109
- this . appState . subscribe ( results => {
110
- this . state = Object . assign ( { } , this . state , results )
111
- } ) ;
112
-
113
109
}
114
110
handleIncrement ( ) {
115
- console . log ( 'CHANGE App' ) ;
116
111
this . counterIntent . incrementCounter ( ) ;
117
112
}
118
113
toggleGreet ( ) {
119
- console . log ( 'CHANGE App' ) ;
120
114
this . greetIntent . toggleGreet ( ) ;
121
115
}
122
- // doesn't work at the moment
116
+
123
117
onChange ( value ) {
124
- console . log ( 'CHANGE App\n' , JSON . stringify ( value , null , 2 ) ) ;
118
+ console . log ( 'CHANGE App' , ' \n', JSON . stringify ( value , null , 2 ) ) ;
125
119
}
126
- }
127
-
128
-
129
120
121
+ }
0 commit comments