Skip to content
This repository was archived by the owner on Feb 17, 2024. It is now read-only.

Commit 4718e37

Browse files
committed
refactor(app): use combineLatest and GetPipe
1 parent e7ae446 commit 4718e37

File tree

1 file changed

+31
-39
lines changed

1 file changed

+31
-39
lines changed

src/app/components/app.ts

+31-39
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
// Angular 2
55
import {Component, View, coreDirectives, onChange, ON_PUSH} from 'angular2/angular2';
66
import {Observable} from 'angular2/src/facade/async';
7+
import {Inject} from 'angular2/di'
78
import {RouteConfig, Router, RouterOutlet, RouterLink} from 'angular2/router';
89
var routerDirectives = [RouterOutlet, RouterLink];
910
import {BrowserLocation} from 'angular2/src/router/browser_location';
@@ -26,9 +27,7 @@ import {GreetModel} from '../models/GreetModel';
2627
selector: 'count',
2728
lifecycle: [onChange],
2829
changeDetection: ON_PUSH,
29-
properties: {
30-
'counter': 'counter'
31-
}
30+
properties: { 'counter': 'counter' }
3231
})
3332
@View({
3433
template: `
@@ -46,25 +45,27 @@ import {GreetModel} from '../models/GreetModel';
4645
`
4746
})
4847
export class Count {
49-
counter: number;
48+
counter: number = 0;
5049
counterIntent: CounterIntent;
5150
constructor(counterIntent: CounterIntent) {
5251
this.counterIntent = counterIntent;
52+
// console.log('counter', this.counter)
5353
}
5454

55-
onChange(value) {
56-
console.log('CHANGE Count\n', JSON.stringify(value, null, 2));
57-
}
5855
incrementCounter() {
5956
this.counterIntent.incrementCounter();
6057
}
58+
59+
onChange(value) {
60+
console.log('CHANGE Count','\n', JSON.stringify(value, null, 2));
61+
}
6162
}
6263

6364

64-
// App: Top Level Component
65+
6566
@Component({
66-
selector: 'app', // without [ ] means we are selecting the tag directly,
67-
lifecycle: [onChange]
67+
selector: 'app',
68+
lifecycle: [onChange],
6869
// changeDetection: ON_PUSH
6970
})
7071
@View({
@@ -74,56 +75,47 @@ export class Count {
7475
7576
<h1 class="title">Hello Reactive Angular 2</h1>
7677
78+
<test-app></test-app>
7779
78-
<count [counter]="state.counter">
80+
81+
<count [counter]="appState | async | get('counter')">
7982
<button (click)="handleIncrement()">Increment Counter from App</button>
8083
</count>
8184
82-
<h2>Greet {{ state.greeting }}</h2>
85+
<h2>Greet {{ appState | async | get('greeting') }}</h2>
8386
<div>
84-
<button (^click)="toggleGreet()">Greet {{ state.greeting }} </button>
87+
<button (^click)="toggleGreet()">Greet {{ appState | async | get('greeting') }} </button>
8588
</div>
86-
<pre>state = {{ state | json }}</pre>
8789
<pre>appState = {{ appState | async | json }}</pre>
8890
`
8991
})
9092
export class App {
9193
state: any;
9294
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)
106107
);
107108

108-
109-
this.appState.subscribe(results => {
110-
this.state = Object.assign({}, this.state, results)
111-
});
112-
113109
}
114110
handleIncrement() {
115-
console.log('CHANGE App');
116111
this.counterIntent.incrementCounter();
117112
}
118113
toggleGreet() {
119-
console.log('CHANGE App');
120114
this.greetIntent.toggleGreet();
121115
}
122-
// doesn't work at the moment
116+
123117
onChange(value) {
124-
console.log('CHANGE App\n', JSON.stringify(value, null, 2));
118+
console.log('CHANGE App', '\n', JSON.stringify(value, null, 2));
125119
}
126-
}
127-
128-
129120

121+
}

0 commit comments

Comments
 (0)