Skip to content

Commit 28ee895

Browse files
docs(website): add angular and vue guides
1 parent f7fdf4e commit 28ee895

File tree

12 files changed

+325
-5
lines changed

12 files changed

+325
-5
lines changed

packages/overmind-website/examples/guide/managinglists/render2.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,19 +57,19 @@ export default connect({
5757
target: 'markup',
5858
code: `
5959
<ul>
60-
<li is="Post" v-for="post in overmind.state.postsList" v-bind:post="post" :key="post.id" />
60+
<post-component v-for="post in overmind.state.postsList" v-bind:post="post" :key="post.id" />
6161
</ul>
6262
`,
6363
},
6464
{
6565
fileName: 'components/Posts.vue (script)',
6666
code: `
6767
import { connect } from '../overmind'
68-
import Post from './Post'
68+
import PostComponent from './Post'
6969
7070
export default connect({
7171
components: {
72-
Post,
72+
PostComponent,
7373
},
7474
})
7575
`,
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
export default () => [
2+
{
3+
fileName: 'overmind/index.ts',
4+
code: `
5+
import { Overmind, TConfig } from 'overmind'
6+
import { createConnect, TConnect } from 'overmind-angular'
7+
import { state } from './state'
8+
import * as actions from './actions'
9+
10+
const config = {
11+
state,
12+
actions
13+
}
14+
15+
declare module 'overmind' {
16+
interface IConfig extends TConfig<typeof config> {}
17+
}
18+
19+
export type Connect = TConnect<typeof config>
20+
21+
const overmind = new Overmind(config)
22+
23+
export const connect = createConnect(overmind)
24+
`,
25+
},
26+
{
27+
fileName: 'components/app.component.ts',
28+
code: `
29+
import { Component } from '@angular/core'
30+
import { connect, Connect } from '../overmind'
31+
32+
@Component({
33+
selector: 'app-root',
34+
templateUrl: './app.component.html',
35+
styleUrls: ['./app.component.css']
36+
})
37+
@connect()
38+
export class AppComponent {
39+
overmind: Connect
40+
title = 'My First Angular App with Overmind!'
41+
}
42+
`,
43+
},
44+
]
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
export default () => [
2+
{
3+
fileName: 'components/app.component.ts',
4+
code: `
5+
import { Component } from '@angular/core'
6+
import { connect, Connect } from '../overmind'
7+
8+
@Component({
9+
selector: 'app-root',
10+
templateUrl: './app.component.html'
11+
})
12+
@connect()
13+
export class AppComponent {
14+
overmind: Connect
15+
disposeMutationListener: () => void
16+
ngOnInit() {
17+
this.disposeMutationListener = this.overmind.addMutationListener((mutation) => {
18+
if (mutation.path === 'currentPage') {
19+
document.querySelector('#app').scrollTop = 0
20+
}
21+
})
22+
}
23+
ngOnDestroy() {
24+
this.disposeMutationListener()
25+
}
26+
}
27+
`,
28+
},
29+
]
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
export default () => [
2+
{
3+
fileName: 'components/app.component.ts',
4+
code: `
5+
import {
6+
Component,
7+
ChangeDetectionStrategy,
8+
ChangeDetectorRef
9+
} from '@angular/core'
10+
import { connect, Connect } from '../overmind'
11+
12+
@Component({
13+
selector: 'app-root',
14+
templateUrl: './app.component.html',
15+
styleUrls: ['./app.component.css'],
16+
changeDetection: ChangeDetectionStrategy.OnPush
17+
})
18+
@connect()
19+
export class AppComponent {
20+
overmind: Connect
21+
constructor(private cdr: ChangeDetectorRef) {}
22+
}
23+
`,
24+
},
25+
]
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
export default () => [
2+
{
3+
fileName: 'components/todo.component.ts',
4+
code: `
5+
import { Component, Input } from '@angular/core'
6+
import { connect } from '../overmind'
7+
import { Todo } from '../overmind/state'
8+
9+
@Component({
10+
selector: 'todos-todo',
11+
template: \`
12+
<li>{{todo.title}}</li>
13+
\`
14+
})
15+
@connect()
16+
export class TodoComponent {
17+
@Input() todo: Todo
18+
}
19+
`,
20+
},
21+
{
22+
fileName: 'components/todos.component.ts',
23+
code: `
24+
import { Component } from '@angular/core'
25+
import { connect } from '../overmind'
26+
27+
@Component({
28+
selector: 'todos-list',
29+
template: \`
30+
<ul>
31+
<todos-todo
32+
*ngFor="let todo of overmind.state.todos"
33+
[todo]="todo"
34+
></todos-todo>
35+
</ul>
36+
\`
37+
})
38+
@connect()
39+
export class ListComponent {}
40+
`,
41+
},
42+
]
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
export default () => [
2+
{
3+
fileName: 'overmind/index.jsx',
4+
code: `
5+
import { Overmind } from 'overmind'
6+
import { createConnect } from 'overmind-vue'
7+
8+
const overmind = new Overmind({
9+
state: {},
10+
actions: {}
11+
})
12+
13+
export const connect = createConnect(overmind)
14+
`,
15+
},
16+
{
17+
fileName: 'components/SomeComponent.vue (template)',
18+
code: `
19+
<div v-on:click="overmind.actions.onClick">
20+
{{overmind.state.foo}}
21+
</div>
22+
`,
23+
},
24+
{
25+
fileName: 'components/SomeComponent.vue (script)',
26+
code: `
27+
import { connect } from '../overmind'
28+
29+
export default connect({})
30+
`,
31+
},
32+
]
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
export default () => [
2+
{
3+
fileName: 'components/SomeComponent.vue (template)',
4+
code: `
5+
<div v-on:click="overmind.actions.onClick">
6+
{{overmind.state.foo}}
7+
</div>
8+
`,
9+
},
10+
{
11+
fileName: 'components/SomeComponent.vue (script)',
12+
code: `
13+
import { connect } from '../overmind'
14+
15+
export default connect({
16+
mounted() {
17+
this.disposeMutationListener = this.overmind.addMutationListener((mutation) => {
18+
if (mutation.path === 'currentPage') {
19+
document.querySelector('#app').scrollTop = 0
20+
}
21+
})
22+
},
23+
destroyed() {
24+
this.disposeMutationListener()
25+
}
26+
})
27+
`,
28+
},
29+
]
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
export default () => [
2+
{
3+
fileName: 'overmind/index.jsx',
4+
code: `
5+
import { Overmind } from 'overmind'
6+
import { createConnect } from 'overmind-vue'
7+
8+
const overmind = new Overmind({
9+
state: {},
10+
actions: {}
11+
})
12+
13+
export const connect = createConnect(overmind)
14+
`,
15+
},
16+
{
17+
fileName: 'components/Todo.vue (template)',
18+
code: `
19+
<li>{{todo.title}}</li>
20+
`,
21+
},
22+
{
23+
fileName: 'components/Todo.vue (script)',
24+
code: `
25+
import { connect } from '../overmind'
26+
27+
export default connect({
28+
props: ["todo"]
29+
})
30+
`,
31+
},
32+
{
33+
fileName: 'components/Todos.vue (template)',
34+
code: `
35+
<ul>
36+
<todo-component
37+
v-for="post in overmind.state.postsList"
38+
v-bind:todo="todo"
39+
:key="todo.id"
40+
/>
41+
</ul>
42+
`,
43+
},
44+
{
45+
fileName: 'components/Todos.vue (script)',
46+
code: `
47+
import { connect } from '../overmind'
48+
import TodoComponent from './Todo'
49+
50+
export default connect({
51+
components: {
52+
TodoComponent
53+
}
54+
})
55+
`,
56+
},
57+
]

packages/overmind-website/guides/beginner/06_usingovermindwithreact.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ If you previously used **componentDidUpdate** to trigger an effect, that is no l
2323

2424
### Passing state as props
2525

26-
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. The devtools will help you identify where any components are left "unconnected".
26+
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".
2727

2828
```marksy
2929
h(Example, { name: "guide/usingovermindwithreact/hoc_passprop" })
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# Using Overmind with Angular
2+
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.
4+
5+
```marksy
6+
h(Example, { name: "guide/usingovermindwithangular/connect" })
7+
```
8+
9+
10+
## Rendering
11+
12+
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.
13+
14+
```marksy
15+
h(Example, { name: "guide/usingovermindwithangular/onpush" })
16+
```
17+
18+
```marksy
19+
h(Notice, null, "The class property has to be named **cdr**. Overmind gives an error if you use onPush strategy without injecting ChangeDetectorRef correctly.")
20+
```
21+
22+
## Passing state as input
23+
24+
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".
25+
26+
```marksy
27+
h(Example, { name: "guide/usingovermindwithangular/passprop" })
28+
```
29+
30+
## Effects
31+
32+
To run effects in components based on changes to state you use the **addMutationListener** function in the lifecycle hooks of Angular.
33+
34+
```marksy
35+
h(Example, { name: "guide/usingovermindwithangular/effect" })
36+
```

0 commit comments

Comments
 (0)