re-frame/docs/Basic-App-Structure.md

72 lines
2.9 KiB
Markdown
Raw Normal View History

2016-08-23 21:07:14 +00:00
## Simpler Apps
To build a re-frame app, you:
2017-04-15 02:19:24 +00:00
- design your app's data structures (data layer)
- write Reagent view functions (domino 5)
- write event handler functions (control layer and/or state transition layer, domino 2)
- write subscription functions (query layer, domino 4)
2016-08-23 21:07:14 +00:00
For simpler apps, you should put code for each layer into separate files:
```
src
2016-08-27 02:41:26 +00:00
├── core.cljs <--- entry point, plus history, routing, etc
2016-08-23 21:07:14 +00:00
├── db.cljs <--- schema, validation, etc (data layer)
2017-04-15 02:19:24 +00:00
├── views.cljs <--- reagent views (view layer)
├── events.cljs <--- event handlers (control/update layer)
└── subs.cljs <--- subscription handlers (query layer)
2016-08-23 21:07:14 +00:00
```
For a living example of this approach, look at the [todomvc example](https://github.com/Day8/re-frame/tree/master/examples/todomvc).
2017-04-15 02:19:24 +00:00
*No really, you should absolutely look at the [todomvc example](https://github.com/Day8/re-frame/tree/master/examples/todomvc) example, as soon as possible. It contains all sorts of tips.*
2016-08-27 02:41:26 +00:00
### There's A Small Gotcha
2016-08-23 21:07:14 +00:00
2017-04-15 02:19:24 +00:00
If you adopt this structure, there's a gotcha.
2016-08-23 21:07:14 +00:00
2016-08-27 02:41:26 +00:00
`events.cljs` and `subs.cljs` will never be `required` by any other
namespaces. To the Google Closure dependency mechanism it appears as
if these two namespaces are not needed and it doesn't load them.
2016-08-23 21:07:14 +00:00
2016-08-27 02:41:26 +00:00
And, if the code does not get loaded, the registrations in these namespaces
2017-04-15 02:19:24 +00:00
never happen. You'll then you'll be puzzled as to why none of your events handlers
2016-08-27 02:41:26 +00:00
are registered.
2016-08-23 21:07:14 +00:00
2016-08-27 02:41:26 +00:00
Once you twig to what's going on, the solution is easy. You must
explicitly `require` both namespaces, `events` and `subs`, in your `core`
namespace. Then they'll be loaded and the registrations will occur
as that loading happens.
2016-08-23 21:07:14 +00:00
## Larger Apps
2017-04-15 02:19:24 +00:00
Assuming your larger apps have multiple "panels" (or "views") which are
2016-08-27 02:41:26 +00:00
relatively independent, you might use this structure:
2016-08-23 21:07:14 +00:00
```
src
2016-08-25 13:35:48 +00:00
├── panel-1
2016-08-24 14:53:37 +00:00
│ ├── db.cljs <--- schema, validation, etc (data layer)
│ ├── subs.cljs <--- subscription handlers (query layer)
│ ├── views.cljs <--- reagent components (view layer)
│ └── events.cljs <--- event handlers (control/update layer)
2016-08-25 13:35:48 +00:00
├── panel-2
2016-08-24 14:53:37 +00:00
│ ├── db.cljs <--- schema, validation. etc (data layer)
│ ├── subs.cljs <--- subscription handlers (query layer)
│ ├── views.cljs <--- reagent components (view layer)
│ └── events.cljs <--- event handlers (control/update layer)
2016-08-23 21:07:14 +00:00
.
.
2016-08-25 13:35:48 +00:00
└── panel-n
2016-08-23 21:07:14 +00:00
```
***
Previous: [Correcting a wrong](SubscriptionsCleanup.md)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
Up: [Index](README.md)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
2016-08-28 15:41:52 +00:00
Next: [Navigation](Navigation.md)
2016-12-20 12:16:53 +00:00
<!-- START doctoc generated TOC please keep comment here to allow auto update -->
<!-- DON'T EDIT THIS SECTION, INSTEAD RE-RUN doctoc TO UPDATE -->
<!-- END doctoc generated TOC please keep comment here to allow auto update -->