Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 119328b

Browse files
authored
Update Readme.md
1 parent 163e81b commit 119328b

File tree

1 file changed

+69
-7
lines changed

1 file changed

+69
-7
lines changed

libs/state/selections/docs/Readme.md

Lines changed: 69 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ This data is consumed in different screens:
2424
interface SelectionScreen1 {
2525
title: string;
2626
sortDirection: 'asc' | 'desc' | 'none';
27-
list: Array<{ id: number }>
27+
sortedList: Array<{ id: number }>
2828
}
2929
```
3030

@@ -33,7 +33,7 @@ interface SelectionScreen1 {
3333
interface SelectionScreen2 {
3434
title: string;
3535
startingDate: Date;
36-
list: { id: number }
36+
filteredList: { id: number }
3737
}
3838
```
3939

@@ -90,18 +90,22 @@ A setup of the compoents class based on `RxState` could look like this:
9090
@Component({
9191
selector: 'app-problem',
9292
template: `
93-
<ng-container>
94-
<h1>{{}}</h1>
93+
<ng-container *rxLet="viewModel$; let vm">
94+
<h1>{{vm.title}} - {{vm.sortDirection}}</h1>
95+
<ul>
96+
<li *ngFor="let item of vm.sortedList">{{item}}</li>
97+
</ul>
9598
</ng-container>
9699
`,
97100
providers: [RxState],
98101
})
99102
export class ProblemComponent {
103+
104+
viewModel$: Observable<ViewModel>; // ???
100105

101106
constructor(private globalState: GlobalState, private state: RxState<Model>) {
102-
this.state.set(title: 'My list')
107+
this.state.set({title: 'My list'})
103108
this.state.connect('products', this.globalState.products$);
104-
105109
}
106110

107111
toggleSort() {
@@ -117,13 +121,71 @@ In a components template we want to render the the UI for the above explained vi
117121
interface SelectionScreen1 {
118122
title: string;
119123
sortDirection: 'asc' | 'desc' | 'none';
120-
list: Array<{ id: number }>
124+
sortedList: Array<{ id: number }>
121125
}
122126
```
123127

128+
A common implementations looks like this:
129+
130+
131+
```typescript
132+
// template removed for brevity
133+
export class ProblemComponent {
134+
135+
private sortedList$ = this.state.select(
136+
selectSlice(['sortDirection', 'list']),
137+
map(() => {
138+
// sort `list` by `sortDirection` to `sortedList` here
139+
return sortedList;
140+
})
141+
);
142+
143+
viewModel$ = this.state.select(
144+
selectSlice(['title', 'sortedList', 'sortDirection'])
145+
)
146+
147+
// BAD: modle viewmodel mix up 👇
148+
constructor(private globalState: GlobalState, private state: RxState<Model & Pick<ViewModel, 'sortedList'>>) {
149+
this.state.set({title: 'My list'})
150+
this.state.connect('products', this.globalState.products$);
151+
// BAD: store derived state 👇
152+
this.state.connect('sortedList', this.sortedList$);
153+
}
154+
155+
// ...
156+
}
157+
158+
```
124159

125160
![Selections (6)](https://user-images.githubusercontent.com/10064416/152422999-db8260f0-69e1-4d99-b6ac-b2b1d043b4b7.png)
126161

162+
By removing the sorted list form the state and moving it into the selection
163+
we can clean up the state's typing and have a nice separation of which data is owned by the component (model) and which data is owned by the template (view model)
164+
165+
```typescript
166+
// template removed for brevity
167+
export class ProblemComponent {
168+
169+
private sortedSlice$ = this.state.select(
170+
selectSlice(['sortDirection', 'list']),
171+
map(({list, sortDirection}) => {
172+
// sort `list` by `sortDirection` to `sortedList` here
173+
return { sortDirection, sortedList };
174+
})
175+
);
176+
177+
// GOOD: Derive view model from model 👇
178+
viewModel$ = smosh({ title: this.state.select('title')}, this.sortedSlice$);
179+
180+
constructor(private globalState: GlobalState, private state: RxState<Model>) {
181+
this.state.set({title: 'My list'});
182+
this.state.connect('products', this.globalState.products$);
183+
}
184+
185+
// ...
186+
}
187+
188+
```
127189

128190
![Selections (7)](https://user-images.githubusercontent.com/10064416/152423026-d23326c2-97d5-4bd0-9015-f498c3fc0e55.png)
129191

0 commit comments

Comments
 (0)