I’m really enjoying Web Development with Clojure, 3rd edition. Thanks to the authors for writing it. There are so many separate moving parts to web development in Clojure/Script, and while very powerful when understood, this book is really a “must read” for anyone new to the ecosystem.
While reading version B8.0 of the book I noticed something in the code examples that I’m having some difficulty understanding. Why do the later versions of the guestbook app need to call vec
on the messages returned from the database?
In the earlier versions (see [1] below) of the app that render the page on the server, there’s no need to call vec
:
[1]
Chapter 3, Think in Terms of Aplication Components > Routing Requests
pdf page: 74
book page: 61
File: https://media.pragprog.com/titles/dswdcloj3/code/guestbook/src/clj/guestbook/routes/home.clj
(defn home-page [request]
(layout/render
request "home.html" {:messages (db/get-messages)}))
But in later versions (see [2] and [3] below) of the guestbook app, which are using client-side rendering, vec
is used:
[2]
Chapter 4, Build the UI with Reagent > Reimplementing the List
pdf page: 100
book page: 88
File: https://media.pragprog.com/titles/dswdcloj3/code/guestbook-reagent/src/clj/guestbook/routes/home.clj
(defn message-list [_]
(response/ok {:messages (vec (db/get-messages))}))
[3]
Chapter 5, Services > Guestbook Messages
pdf page: 113
book page: 101
File: https://media.pragprog.com/titles/dswdcloj3/code/guestbook-services-1/src/clj/guestbook/messages.clj
(defn message-list []
{:messages (vec (db/get-messages))})
When I removed the call to vec
in [3], I noticed that the messages are displayed in ascending descending order of :id
, which causes the more recent messages to show at the top of the page, instead of at the bottom as it does when vec
is called.
I’m not clear how vec
has this effect? When I try (vec (db/get-messages))
and just (db/get-messages)
at the REPL, the messages are in the same order.