Skip to content

Commit 19305f0

Browse files
authored
Edit guide to match JS DSL snippet (#72)
* Add examples and fix "browse" behavior in guide * Remove examples - these now live in cookbook
1 parent b029f20 commit 19305f0

File tree

1 file changed

+14
-8
lines changed

1 file changed

+14
-8
lines changed

GUIDE.md

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ data MyRoute
3838
= PostIndex
3939
| Post PostId
4040
| PostEdit PostId
41-
| PostBrowse String String
41+
| PostBrowse Int String
4242
```
4343

4444
By using a data type, we can use `case` analysis to guarantee that we've
@@ -111,7 +111,7 @@ And now finally, we need to extract multiple segments for `PostBrowse`.
111111
```purescript
112112
postBrowse :: Match MyRoute
113113
postBrowse =
114-
PostBrowse <$> (lit "posts" *> str) <*> str
114+
PostBrowse <$> (lit "posts" *> lit "browse" *> int) <*> str
115115
```
116116

117117
The `<*>` combinator has arrows on both sides because we want both values.
@@ -150,7 +150,7 @@ myRoute = oneOf
150150
[ PostIndex <$ lit "posts"
151151
, Post <$> (lit "posts" *> int)
152152
, PostEdit <$> (lit "posts" *> int) <* lit "edit"
153-
, PostBrowse <$> (lit "posts" *> str) <*> str
153+
, PostBrowse <$> (lit "posts" *> lit "browse" *> int) <*> str
154154
]
155155
```
156156

@@ -166,7 +166,7 @@ myRoute =
166166
[ pure PostIndex
167167
, Post <$> int
168168
, PostEdit <$> int <* lit "edit"
169-
, PostBrowse <$> str <*> str
169+
, PostBrowse <$> (lit "browse" *> int) <*> str
170170
]
171171
```
172172

@@ -185,7 +185,7 @@ myRoute =
185185
[ PostIndex <$ end
186186
, Post <$> int <* end
187187
, PostEdit <$> int <* lit "edit" <* end
188-
, PostBrowse <$> str <*> str <* end
188+
, PostBrowse <$> (lit "browse" *> int) <*> str <* end
189189
]
190190
```
191191

@@ -199,7 +199,7 @@ myRoute =
199199
lit "posts" *> oneOf
200200
[ PostEdit <$> int <* lit "edit"
201201
, Post <$> int
202-
, PostBrowse <$> str <*> str
202+
, PostBrowse <$> (lit "browse" *> int) <*> str
203203
, pure PostIndex
204204
] <* end
205205
```
@@ -217,7 +217,7 @@ myRoute =
217217
root *> lit "posts" *> oneOf
218218
[ PostEdit <$> int <* lit "edit"
219219
, Post <$> int
220-
, PostBrowse <$> str <*> str
220+
, PostBrowse <$> (lit "browse" *> int) <*> str
221221
, pure PostIndex
222222
] <* end
223223
```
@@ -234,14 +234,17 @@ matchMyRoute = match myRoute
234234
test1 = matchMyRoute "/posts"
235235
test2 = matchMyRoute "/posts/12"
236236
test3 = matchMyRoute "/posts/12/edit"
237+
test3 = matchMyRoute "/posts/browse/2004/June"
237238
test4 = matchMyRoute "/psots/bad"
238239
```
239240

240241
## Routing events with `Routing.Hash`
241242

242243
Now that we have a parser, we'll want to respond to events and fire a
243244
callback like in our original example. `purescript-routing` supports
244-
hash-based routing via `Routing.Hash`.
245+
hash-based routing via `Routing.Hash`. Hash-based routing uses anchors
246+
(`#` or "hash" character) to specify the routes.
247+
For example: `www.example.com/#posts/12/edit`.
245248

246249
```purescript
247250
import Routing.Hash (matches)
@@ -285,6 +288,9 @@ Alternatively, we could explicitly add a `NotFound` constructor to `MyRoute`.
285288

286289
## Routing events with `Routing.PushState`
287290

291+
PushState-based routing *avoids* the use of anchors (`#`) to specify the routes.
292+
For example: `www.example.com/posts/12/edit`.
293+
288294
Routing with `Routing.PushState` is similar to hash-based routing except that
289295
we must first create an interface. Browsers don't handle location events
290296
directly, so the interface needs to do some bookkeeping of it's own for

0 commit comments

Comments
 (0)