Skip to content

Commit 79bbe66

Browse files
docs(website): new docs for angular
1 parent e2b5e45 commit 79bbe66

File tree

8 files changed

+98
-76
lines changed

8 files changed

+98
-76
lines changed

packages/overmind-website/examples/guide/usingovermindwithangular/connect.ts

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ export default () => [
33
fileName: 'overmind/index.ts',
44
code: `
55
import { Overmind, IConfig } from 'overmind'
6-
import { createConnect, IConnect } from 'overmind-angular'
6+
import { createService } from 'overmind-angular'
7+
import { Injectable } from '@angular/core'
78
import { state } from './state'
89
import * as actions from './actions'
910
@@ -16,29 +17,33 @@ declare module 'overmind' {
1617
interface Config extends IConfig<typeof config> {}
1718
}
1819
19-
export interface Connect extends IConnect<typeof config> {}
20-
2120
const overmind = new Overmind(config)
2221
23-
export const connect = createConnect(overmind)
22+
@Injectable()
23+
export class OvermindService extends createService(overmind) {}
2424
`,
2525
},
2626
{
2727
fileName: 'components/app.component.ts',
2828
code: `
29-
import { Component } from '@angular/core'
30-
import { connect, Connect } from '../overmind'
29+
import { Component, ChangeDetectionStrategy } from '@angular/core'
30+
import { OvermindService } from '../overmind'
3131
3232
@Component({
3333
selector: 'app-root',
34-
templateUrl: './app.component.html',
35-
styleUrls: ['./app.component.css']
34+
template: \`
35+
<div *ngIf="state$ | async as state">
36+
<h1 (click)="actions.changeTitle()">{{ state.title }}</h1>
37+
</div>
38+
\`,
39+
providers: [OvermindService],
40+
changeDetection: ChangeDetectionStrategy.OnPush
3641
})
37-
@connect()
3842
export class AppComponent {
39-
overmind: Connect
40-
title = 'My First Angular App with Overmind!'
41-
}
43+
state$ = this.overmind.select()
44+
actions: this.overmind.actions
45+
constructor(private overmind: OvermindService) {}
46+
},
4247
`,
4348
},
4449
]
Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,25 @@
11
export default () => [
22
{
3-
fileName: 'components/admin.component.ts',
3+
fileName: 'components/app.component.ts',
44
code: `
5-
import { Component } from '@angular/core'
6-
import { connect } from '../overmind'
5+
import { Component, ChangeDetectionStrategy } from '@angular/core'
6+
import { OvermindService } from '../overmind'
77
88
@Component({
9-
selector: 'admin-root',
10-
templateUrl: './admin.component.html',
11-
styleUrls: ['./admin.component.css']
9+
selector: 'app-root',
10+
template: \`
11+
<div *ngIf="state$ | async as state">
12+
<h1 (click)="actions.changeAdmin()">{{ state.adminTitle }}</h1>
13+
</div>
14+
\`,
15+
providers: [OvermindService],
16+
changeDetection: ChangeDetectionStrategy.OnPush
1217
})
13-
@connect(({ state, actions, effects }) => ({
14-
state: state.admin,
15-
actions: actions.admin
16-
}))
17-
export class AdminComponent {}
18+
export class AppComponent {
19+
state$ = this.overmind.select(state => state.admin)
20+
actions: this.overmind.actions.admin
21+
constructor(private overmind: OvermindService) {}
22+
},
1823
`,
1924
},
2025
]

packages/overmind-website/examples/guide/usingovermindwithangular/effect.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,15 @@ export default () => [
33
fileName: 'components/app.component.ts',
44
code: `
55
import { Component } from '@angular/core'
6-
import { connect, Connect } from '../overmind'
6+
import { OvermindService } from '../overmind'
77
88
@Component({
99
selector: 'app-root',
1010
templateUrl: './app.component.html'
1111
})
12-
@connect()
1312
export class AppComponent {
14-
overmind: Connect
1513
disposeMutationListener: () => void
14+
constructor (private overmind: OvermindService) {}
1615
ngOnInit() {
1716
this.disposeMutationListener = this.overmind.addMutationListener((mutation) => {
1817
if (mutation.path === 'currentPage') {

packages/overmind-website/examples/guide/usingovermindwithangular/onpush.ts

Lines changed: 0 additions & 25 deletions
This file was deleted.

packages/overmind-website/examples/guide/usingovermindwithangular/passprop.ts

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,41 +2,46 @@ export default () => [
22
{
33
fileName: 'components/todo.component.ts',
44
code: `
5-
import { Component, Input } from '@angular/core'
6-
import { connect } from '../overmind'
5+
import { Component, Input, ChangeDetectionStrategy } from '@angular/core'
6+
import { OvermindService } from '../overmind'
77
import { Todo } from '../overmind/state'
88
99
@Component({
1010
selector: 'todos-todo',
11+
changeDetection: ChangeDetectionStrategy.OnPush,
1112
template: \`
12-
<li>{{todo.title}}</li>
13+
<li ngIf="todo$ | async as todo">{{ todo.title }}</li>
1314
\`
1415
})
1516
@connect()
1617
export class TodoComponent {
17-
@Input() todo: Todo
18+
@Input() index: number
19+
todo$ = this.overmind.select(state => state.todos[this.index])
20+
constructor(private overmind: OvermindService) {}
1821
}
1922
`,
2023
},
2124
{
2225
fileName: 'components/todos.component.ts',
2326
code: `
24-
import { Component } from '@angular/core'
25-
import { connect } from '../overmind'
27+
import { Component, ChangeDetectionStrategy } from '@angular/core'
28+
import { OvermindService } from '../overmind'
2629
2730
@Component({
2831
selector: 'todos-list',
2932
template: \`
30-
<ul>
31-
<todos-todo
32-
*ngFor="let todo of overmind.state.todos"
33-
[todo]="todo"
34-
></todos-todo>
35-
</ul>
33+
<ul *ngIf="state$ | async as state">
34+
<todos-todo
35+
*ngFor="let todo of state.todos; index as i;"
36+
[index]="i"
37+
></todos-todo>
38+
</ul>
3639
\`
3740
})
38-
@connect()
39-
export class ListComponent {}
41+
export class ListComponent {
42+
state$ = this.overmind.select()
43+
constructor(private overmind: OvermindService)
44+
}
4045
`,
4146
},
4247
]
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
export default () => [
2+
{
3+
fileName: 'components/app.component.ts',
4+
code: `
5+
import { Component, ChangeDetectionStrategy } from '@angular/core'
6+
import { OvermindService } from '../overmind'
7+
8+
@Component({
9+
selector: 'app-root',
10+
template: \`\`,
11+
providers: [OvermindService],
12+
changeDetection: ChangeDetectionStrategy.OnPush
13+
})
14+
@OvermindService.Track
15+
export class AppComponent {
16+
state$ = this.overmind.select()
17+
actions: this.overmind.actions
18+
constructor(private overmind: OvermindService) {}
19+
},
20+
`,
21+
},
22+
]

packages/overmind-website/examples/templates.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ export const useOvermind = createHook(overmind)
1717
`,
1818
angular: (config) => `
1919
import { Overmind, IConfig } from 'overmind'
20-
import { createConnect, IConnect } from 'overmind-angular'
20+
import { createService } from 'overmind-angular'
21+
import { Injectable } from '@angular/core'
2122
${config.trim()}
2223
2324
// For explicit typing check the Typescript guide
@@ -29,7 +30,8 @@ export interface Connect extends IConnect<typeof config> {}
2930
3031
export const overmind = new Overmind(config)
3132
32-
export const connect = createConnect(overmind)
33+
@Injectable()
34+
export class OvermindService extends createService(overmind) {}
3335
`,
3436
vue: (config) => ``,
3537
}

packages/overmind-website/guides/beginner/07_usingovermindwithangular.md

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,48 @@
11
# Using Overmind with Angular
22

3-
Using Overmind with Angular is straight forward. You create a **connect** decorator and use it with whatever component. An **overmind** property is added to the component and there you can access state, actions and listen to mutations to do effects.
3+
Using Overmind with Angular is straight forward. You create a **service** and use it with whatever component. By using the **select** API you create observable state.
4+
5+
```marksy
6+
h(Notice, null, "Make sure that your transpile target is configured to **ES2015** or later. This allows you to extend the service correctly")
7+
```
8+
9+
Let us have a look at how you create the service an expose it to components:
410

511
```marksy
612
h(Example, { name: "guide/usingovermindwithangular/connect" })
713
```
814

9-
You can also expose parts of the configuration on custom properties of the component:
15+
You can also expose parts of the configuration:
1016

1117
```marksy
1218
h(Example, { name: "guide/usingovermindwithangular/connect_custom" })
1319
```
1420

1521
You can now access the **admin** state and actions directly with **state** and **actions**.
1622

17-
## Rendering
23+
## Track components
1824

19-
When you connect Overmind to a component it will use its lifecycle hooks to manage updates. By default you really do not have to think about this as it relies on Angulars default change detection strategy.You can change this behaviour though. By adding the **onPush** strategy and injecting the **ChangeDetectorRef**, the component will only update when it receives input from its parents or Overmind tells it to update due to tracked state has changed.
25+
Optionally you can also track the component itself. The **service** exposes a decorator called **Track**. This will allow you to follow what components are looking at state, what exact state they are looking at and when they update, using the devtools.
2026

2127
```marksy
22-
h(Example, { name: "guide/usingovermindwithangular/onpush" })
28+
h(Example, { name: "guide/usingovermindwithangular/track" })
2329
```
2430

25-
```marksy
26-
h(Notice, null, "The class property has to be named **cdr**. Overmind gives an error if you use onPush strategy without injecting ChangeDetectorRef correctly.")
27-
```
31+
## Rendering
32+
33+
When you connect Overmind to your component and expose state you do not have to think about how much state you expose. The exact state that is being accessed in the template is the state that will be tracked. That means you can expose all the state of the application to all your components without worrying about performance.
34+
2835

2936
## Passing state as input
3037

31-
If you pass a state object or array as a property to a child component you will also in the child component need to **connect**. This ensures that the property you passed is tracked within that component, even though you do not access any state or actions from Overmind. The devtools will help you identify where any components are left "unconnected".
38+
When you pass state objects or arrays as input to a child component that state will by default be tracked on the component passing it a long, which you can also see in the devtools. If you want to effectively pass state as input you should rather pass a reference so that the child component can connect to it.
3239

3340
```marksy
3441
h(Example, { name: "guide/usingovermindwithangular/passprop" })
3542
```
3643

44+
What is important to understand here is that Overmind is **not** immutable. That means if you would change any property on any todo, only the component actually looking at the todo will render. The list is untouched.
45+
3746
## State effects
3847

3948
To run effects in components based on changes to state you use the **addMutationListener** function in the lifecycle hooks of Angular.

0 commit comments

Comments
 (0)