A “model” in Ema represents the state to use to generate your site. It could be as simple as a variable, or it could be a list of parsed Markdown files (as in the case of a weblog). Ema’s model is also conceptually similar to Elm's model, in that - changing the model automatically changes the view.
Here’s an example model:
newtype BlogPosts = BlogPosts (Map Slug Text}
BlogPosts is the model type. If we are generating a weblog site, then all the “data” we need is loaded into memory as a value of
Modifying the model
Ema’s dev server supports hot reload; it will observe changes to your model, in addition to code. To facilitate this you will manage your model as a LVar. The
runEma function (described here) takes an IO action that gets
LVar model as an argument.
runEma render $ \_act model -> forever $ do LVar.set model =<< liftIO getCurrentTime liftIO $ threadDelay $ 1 * 1000000
In this contrived example (full code here), we are using
UTCTime as the model. We set the initial value using
LVar.set, and then continually update the current time every second. Every time the model gets updated, the web browser will hot reload to display the up to date value. For the
BlogPosts model, you would typically use fsnotify to monitor changes to the underlying Markdown files, or even better use the `unionmount` library.
If your model takes on a database-like structure and query functionality, you might find
IxSet to be an useful library. The ixset-typed package (rather than the “ixset” package) is the recommended starting point, and a tutorial can be found here.
Next, we will talk about routes.