2019-10-27 01:49:52 +03:00
|
|
|
# ryannhg/elm-spa
|
2019-10-16 07:52:54 +03:00
|
|
|
> an experiment for creating single page apps with Elm!
|
2019-10-05 01:16:02 +03:00
|
|
|
|
2019-10-08 08:22:17 +03:00
|
|
|
|
2019-10-16 07:52:54 +03:00
|
|
|
### try it out
|
2019-10-05 01:16:02 +03:00
|
|
|
|
2019-10-16 07:52:54 +03:00
|
|
|
1. `npm install`
|
2019-10-05 01:16:02 +03:00
|
|
|
|
2019-10-16 07:52:54 +03:00
|
|
|
1. `npm run dev`
|
2019-10-05 01:16:02 +03:00
|
|
|
|
|
|
|
|
2019-10-16 07:52:54 +03:00
|
|
|
### overview
|
2019-10-05 01:16:02 +03:00
|
|
|
|
2019-10-08 08:22:17 +03:00
|
|
|
```elm
|
|
|
|
module Main exposing (main)
|
2019-10-05 01:16:02 +03:00
|
|
|
|
2019-10-16 07:52:54 +03:00
|
|
|
import Application exposing (Application)
|
|
|
|
import Generated.Pages as Pages
|
2019-10-26 20:06:16 +03:00
|
|
|
import Generated.Route as Route
|
|
|
|
import Global
|
2019-10-05 01:16:02 +03:00
|
|
|
|
2019-10-08 08:22:17 +03:00
|
|
|
|
2019-10-26 20:06:16 +03:00
|
|
|
main : Application Global.Flags Global.Model Global.Msg Pages.Model Pages.Msg
|
2019-10-08 08:22:17 +03:00
|
|
|
main =
|
2019-10-16 07:52:54 +03:00
|
|
|
Application.create
|
|
|
|
{ routing =
|
2019-10-26 20:06:16 +03:00
|
|
|
{ routes = Route.routes
|
|
|
|
, toPath = Route.toPath
|
|
|
|
, notFound = Route.NotFound ()
|
|
|
|
}
|
|
|
|
, global =
|
|
|
|
{ init = Global.init
|
|
|
|
, update = Global.update
|
|
|
|
, subscriptions = Global.subscriptions
|
|
|
|
}
|
2019-10-30 03:25:18 +03:00
|
|
|
, page = Pages.page
|
2019-10-08 23:06:03 +03:00
|
|
|
}
|
|
|
|
```
|
|
|
|
|
2019-10-26 20:06:16 +03:00
|
|
|
### keep your pages simple
|
|
|
|
|
2019-10-27 01:53:14 +03:00
|
|
|
(Instead of making everything an [elm-fork-knife](https://youtu.be/RN2_NchjrJQ?t=2362)!)
|
2019-10-26 20:06:16 +03:00
|
|
|
|
|
|
|
- [`Pages.Index`](./example/src/Pages/Index.elm)
|
|
|
|
|
|
|
|
```elm
|
|
|
|
page =
|
|
|
|
Page.static
|
|
|
|
{ view = view
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
|
|
|
- [`Pages.Counter`](./example/src/Pages/Counter.elm)
|
|
|
|
|
|
|
|
```elm
|
|
|
|
page =
|
|
|
|
Page.sandbox
|
|
|
|
{ init = init
|
|
|
|
, update = update
|
|
|
|
, view = view
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
|
|
|
- [`Pages.Random`](./example/src/Pages/Random.elm)
|
|
|
|
|
|
|
|
```elm
|
|
|
|
page =
|
|
|
|
Page.element
|
|
|
|
{ init = init
|
|
|
|
, update = update
|
|
|
|
, subscriptions = subscriptions
|
|
|
|
, view = view
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
|
|
|
- [`Pages.SignIn`](./example/src/Pages/SignIn.elm)
|
|
|
|
|
|
|
|
```elm
|
|
|
|
page =
|
|
|
|
Page.component
|
|
|
|
{ init = init
|
|
|
|
, update = update
|
|
|
|
, subscriptions = subscriptions
|
|
|
|
, view = view
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
|
|
|
### _and_ your top level easy to read!
|
2019-10-08 23:06:03 +03:00
|
|
|
|
2019-10-26 20:06:16 +03:00
|
|
|
```elm
|
|
|
|
init appRoute =
|
|
|
|
case appRoute of
|
|
|
|
Route.Index route ->
|
|
|
|
index.init route
|
|
|
|
|
|
|
|
Route.Counter route ->
|
|
|
|
counter.init route
|
|
|
|
|
|
|
|
Route.Random route ->
|
|
|
|
random.init route
|
|
|
|
```
|
|
|
|
|
|
|
|
( It's like magic, but actually it's just functions. )
|
2019-10-08 23:06:03 +03:00
|
|
|
|
|
|
|
|
2019-10-26 20:06:16 +03:00
|
|
|
### the folder structure
|
2019-10-05 04:25:25 +03:00
|
|
|
|
2019-10-26 20:10:39 +03:00
|
|
|
- [Example](./example/src)
|
|
|
|
|
|
|
|
```elm
|
2019-10-26 20:06:16 +03:00
|
|
|
src/
|
2019-10-26 20:10:39 +03:00
|
|
|
Api/ -- for backend things
|
|
|
|
Components/ -- reusable ui
|
|
|
|
Data/ -- types used everywhere
|
|
|
|
Layouts/ -- shared views
|
|
|
|
Pages/ -- all your pages
|
|
|
|
Utils/ -- helpers
|
|
|
|
Global.elm -- shared app state
|
|
|
|
Main.elm -- the entrypoint
|
2019-10-26 20:06:16 +03:00
|
|
|
```
|