Skip to content

Commit d46c9bb

Browse files
committed
Updated docs to include last changes
1 parent ac97eeb commit d46c9bb

File tree

1 file changed

+20
-14
lines changed

1 file changed

+20
-14
lines changed

docs/rendering.md

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,18 @@ mount :: forall model message. QuerySelector -> Application model message -> Eff
2424
```
2525
The first parameter is a css selector used as mount point. Under the hood, Flame uses the [snabbdom](https://github.com/snabbdom/snabbdom) virtual DOM.
2626

27+
#### Hooks
28+
29+
Snabbdom provides [hooks](https://github.com/snabbdom/snabbdom#hooks) to inject code into the DOM lifecycle. Since hooks directly manipulate virtual nodes, the functions exported by `Flame.Renderer.Hook` expect foreign interface callbacks, e.g.:
30+
```haskell
31+
-- | Foreign VNode hook function with two parameters
32+
type HookFn2 = EffectFn2 VNode VNode Unit
33+
34+
-- | Attaches a hook for a vnode element been patched
35+
atPostpatch :: forall message. HookFn2 -> NodeData message
36+
```
37+
These functions can be passed to elements in your views in the same way as attributes.
38+
2739
### Server side rendering
2840

2941
We can render a Flame application as a string markup on server side in two different ways:
@@ -40,30 +52,24 @@ which can be used to generate markup as a string, e.g., for a static page or web
4052

4153
The module `Flame` provides
4254
```haskell
43-
preMount :: forall model m message. Generic model m => EncodeRep m => QuerySelector -> PreApplication model message -> Effect String
44-
```
45-
which can used to render on server side the initial state of an application. On client side, we can use
46-
```haskell
47-
resumeMount :: forall model m message. Generic model m => DecodeRep m => QuerySelector -> ResumedApplication model message -> Effect (Channel (Array message))
48-
```
49-
to install event handlers in the pre rendered markup. The `Generic` constraint is necessary since Flame will serialize the initial state of `PreApplication`
50-
```haskell
5155
type PreApplication model message = {
5256
init :: model,
5357
view :: model -> Html message
5458
}
55-
```
56-
into the rendered markup. For this reason, the `QuerySelector` passed to `preMount` and `resumeMount` must match -- otherwise the application will crash with an exception. To avoid diffing issues, the same `view` function should be passed to both function records as well.
5759

58-
`ResumedApplication` is defined as
60+
preMount :: forall model message. SerializeModel model => QuerySelector -> PreApplication model message -> Effect String
61+
```
62+
which can used to render on server side the initial state of an application. On client side, we can use
5963
```haskell
6064
type ResumedApplication model message = {
61-
init :: Array (Aff (Maybe message)),
65+
init :: Array (Aff (Maybe message)), -- only the (optional) initial message to be raised
6266
view :: model -> Html message,
63-
update :: model -> message -> Tuple model (Array (Aff (Maybe message)))
67+
update :: model -> message -> Tuple model (Array (Aff (Maybe message))) --update is only available client side
6468
}
69+
70+
resumeMount :: forall model message. UnserializeModel model => QuerySelector -> ResumedApplication model message -> Effect (Channel (Array message))
6571
```
66-
Notice how `init` only contains the initial messages to be raised. The `update` function responsible for handling events is added on client side as well.
72+
to install event handlers in the pre rendered markup. The `SerializeModel`/`UnserializeModel` type class automatically serialize the initial state as JSON, provided it is either a record or has a `Generic` instance. The `QuerySelector` passed to `preMount` and `resumeMount` must match -- otherwise the application will crash with an exception. To avoid diffing issues, the same `view` function should be used on the server and client side as well.
6773

6874
See the [Dice application](https://github.com/easafe/purescript-flame/tree/master/examples/EffectList/ServerSideRendering) for an example of how to pre render an application on server side.
6975

0 commit comments

Comments
 (0)