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

Commit 22e07d5

Browse files
committed
refactor(app): merge models and maintain appState
1 parent f232865 commit 22e07d5

File tree

1 file changed

+42
-29
lines changed

1 file changed

+42
-29
lines changed

src/app/components/app.ts

Lines changed: 42 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {Observable} from 'angular2/src/facade/async';
77
import {RouteConfig, Router, RouterOutlet, RouterLink} from 'angular2/router';
88
var routerDirectives = [RouterOutlet, RouterLink];
99
import {BrowserLocation} from 'angular2/src/router/browser_location';
10+
import * as Rx from 'rx';
1011

1112
// A simple example of a Component using a Service
1213
// import {Todo} from './todo';
@@ -17,6 +18,9 @@ import {BrowserLocation} from 'angular2/src/router/browser_location';
1718
import {CounterIntent} from '../intents/CounterIntent';
1819
import {CounterModel} from '../models/CounterModel';
1920

21+
import {GreetIntent} from '../intents/GreetIntent';
22+
import {GreetModel} from '../models/GreetModel';
23+
2024

2125
@Component({
2226
selector: 'count',
@@ -38,8 +42,8 @@ export class Count {
3842
this.counterIntent = counterIntent;
3943
}
4044

41-
onChange(wat) {
42-
console.log('CHANGE', wat);
45+
onChange(value) {
46+
console.log('CHANGE Count', value);
4347
}
4448
incrementCounter() {
4549
this.counterIntent.incrementCounter();
@@ -55,47 +59,56 @@ export class Count {
5559
})
5660
@View({
5761
// needed in order to tell Angular's compiler what's in the template
58-
directives: [ routerDirectives, Count ],
62+
directives: [ routerDirectives, coreDirectives, Count ],
5963
template: `
6064
61-
<h1 class="title">Hello {{ name }}</h1>
65+
<h1 class="title">{{ state.greeting }}</h1>
6266
6367
6468
<count [counter]="state.counter"></count>
6569
<button (click)="handleIncrement()">Increment Counter from App</button>
6670
67-
68-
69-
<pre>AppState = {{ model.subject | async | json }}</pre>
71+
<h2>Greet {{ state.greeting }}</h2>
72+
<div>
73+
<button (^click)="toggleGreet()">Greet {{ state.greeting }} </button>
74+
</div>
75+
<pre>AppState = {{ state | json }}</pre>
7076
`
7177
})
7278
export class App {
73-
name: string;
74-
state: Object;
75-
model: CounterModel; counterIntent: CounterIntent;
76-
constructor(model: CounterModel, counterIntent: CounterIntent) {
77-
this.model = model; this.counterIntent = counterIntent;
78-
79-
this.name = 'Angular 2';
80-
this.state = { counter: 0 };
81-
82-
this.model.subject.observer({ next: (state) => this.state = state });
83-
84-
// var AppObservable = Rx.Observable.combineLatest([
85-
// Counter.subject,
86-
// OtherModel.subject
87-
// ],
88-
// (Counter, OtherModel) => {
89-
// return { Counter, OtherModel
90-
// };
91-
// }
92-
// );
79+
state: any;
80+
// public isn't working for me here :/
81+
counter: CounterModel; counterIntent: CounterIntent;
82+
greet: GreetModel; greetIntent: GreetIntent;
83+
constructor(counter: CounterModel, counterIntent: CounterIntent,
84+
greet: GreetModel, greetIntent: GreetIntent) {
85+
this.counter = counter; this.counterIntent = counterIntent;
86+
this.greet = greet; this.greetIntent = greetIntent;
87+
88+
this.state = {};
89+
90+
var appState = Rx.Observable.merge(
91+
this.counter.subject.toRx(),
92+
this.greet.subject.toRx()
93+
);
94+
95+
96+
appState.subscribe(results => {
97+
this.state = Object.assign({}, this.state, results)
98+
});
99+
93100
}
94101
handleIncrement() {
102+
console.log('CHANGE App');
95103
this.counterIntent.incrementCounter();
96104
}
97-
onChange(wat) {
98-
console.log('CHANGE TOP', wat);
105+
toggleGreet() {
106+
console.log('CHANGE App');
107+
this.greetIntent.toggleGreet();
108+
}
109+
// doesn't work at the moment
110+
onChange(value) {
111+
console.log('CHANGE App', value);
99112
}
100113
}
101114

0 commit comments

Comments
 (0)