Database Reference
In-Depth Information
How it works…
A lot took place in this recipe, and there's a lot that we need to talk about in a little
more detail.
First, we need to talk about ClojureScript. If we open up the generated ile,
resources/js/
script.js
, we'll see a lot that's not in our code. Why is there so much more in there?
The explanation is pretty simple. ClojureScript doesn't just target JavaScript as a compilation
platform. It targets the
Google-Closure-compiler-advanced-mode
JavaScript. Because
of this, it includes most of the Google Closure libraries, so we have these immediately
available, and we'll use them in the upcoming recipes. Also, we can run our output through
the Closure compilers by changing the
:optimizations
parameter in the
:cljsbuild
options, and it will shake out the parts of the code that we don't use. This not only includes
our code but also the parts of the Clojure libraries and the Google Closure libraries that we
don't use. In the end, this is a huge win. Unfortunately, not all libraries can be used with the
Closure compiler's advanced mode; D3, for example, requires a ile that lists all the functions
attempt to provide this interface ile for the Closure compiler.
Also, note that the
hello
function is annotated with the
^:export
metadata. This signals
the ClojureScript compiler that it needs to make this function available to the outside world.
This is a good, lightweight way of enforcing scoping according to JavaScript's best practices.
Also, inside the
hello
function, the
alert
function is called from the
js
namespace
(
js/alert
). This namespace represents the global JavaScript context. Whenever we call
a JavaScript object from the global context (when working in the
window
browser), we have
to preix it with the
js/
namespace in ClojureScript.
Next, we should take a look at the
:cljsbuild
coniguration in the
project.clj
ile.
This coniguration section deines the build information. In this case, we tell it to compile
everything in
src-cljs/
to
js/script.js
.
Finally, I will also make a note here about the worklow. If you're keeping count, you probably
have at least three terminal or console windows open. One for the Ring server, one for
the ClojureScript compiler, and one or more for the REPL or shell to work in. That's what I
have going right now. If you can use a terminal multiplexer such as tmux (
http://tmux.
can simplify things. You can set the Ring server and ClojureScript compiler running in a
background window and just check them for errors periodically.