Backbone && Clojurescript
What is clojurescript?
ClojureScript is a new compiler for Clojure that targets JavaScript. It is designed to emit JavaScript code which is compatible with the advanced compilation mode of the Google Closure optimizing compiler.
Why clojurescipt?
- JavaScript Everywhere. It’s the assembly language of web.
- But JavaScript is not robust.
- Clojure is robust.
- Clojurescript: write clojure code, compile it to javascript code!
Backbone.js!
Backbone.js gives structure to web applications by providing models with key-value binding and custom events, collections with a rich API of enumerable functions, views with declarative event handling, and connects it all to your existing API over a RESTful JSON interface.
Write backbone.js with clojurescript!
Manage Dependencies
Q: What do we need?
A:
- :dependencies
- [org.clojure/clojurescript “0.0-2342”]
- :plugins
- [lein-bower “0.5.1”]
- [lein-cljsbuild “1.0.3”]
- :bower-dependencies
- [bootstrap “3.2.0”]
- [jquery “2.1.1”]
- [backbone “1.1.2”]
- :bower
- :directory “resources/public/js/lib”
- cljsbuild
Aggregate all the above (project.clj
):
After running lein deps
and lein bower install
, we have installed all:
$ lein bower ls
beanstalk-console#0.1.0-SNAPSHOT /Users/soasme/space/soft/beanstalk-console
├─┬ backbone#1.1.2
│ └── underscore#1.7.0
├─┬ bootstrap#3.2.0
│ └── jquery#2.1.1
└── jquery#2.1.1
For now, we have install clojure dependencies, clojurescript, bower dependencies, and a task for building all the cljs into js. Let’s step far!
Write view (using hiccup)
Notice that, before include main.js
, it’s important to load backbone first.
To include backbone, include underscore first.
To include bootstrap, include jQuery first. :)
Write Controller
Write backbone code!
As configured before, all the cljs code should be put into src-cljs
folder
(:source-paths ["src-cljs"]
), and all the cljs code will be compiled into
resources/public/dist/js/main.js
file.
Now we need to create new file src-cljs/beanstalk-console/main.cljs
:
This is coming from backbone document:
Events is a module that can be mixed in to any object, giving the object the ability to bind and trigger custom named events. Events do not have to be declared before they are bound, and may take passed arguments. For example:
var object = {};
_.extend(object, Backbone.Events);
object.on("alert", function(msg) {
alert("Triggered " + msg);
});
object.trigger("alert", "an event");
Run lein cljsbuild auto
, it will execute all the cljsbuild task and watch modifying:
beanstalk-console [master●] % lein cljsbuild auto
Compiling ClojureScript.
Compiling "resources/public/dist/js/main.js" from ["src-cljs"]...
Successfully compiled "resources/public/dist/js/main.js" in 10.433 seconds.
Compiling "resources/public/dist/js/main.js" from ["src-cljs"]...
Successfully compiled "resources/public/dist/js/main.js" in 0.625 seconds.
Compiling "resources/public/dist/js/main.js" from ["src-cljs"]...
Successfully compiled "resources/public/dist/js/main.js" in 0.508 seconds.
Compiling "resources/public/dist/js/main.js" from ["src-cljs"]...
Successfully compiled "resources/public/dist/js/main.js" in 0.32 seconds.
Compiling "resources/public/dist/js/main.js" from ["src-cljs"]...
Successfully compiled "resources/public/dist/js/main.js" in 0.343 seconds.
Compiling "resources/public/dist/js/main.js" from ["src-cljs"]...
Successfully compiled "resources/public/dist/js/main.js" in 0.29 seconds.
Open new console, run lein ring server-headless
and happy visiting
http://localhost:3000
!
Step further!
All in all, it’s really simple exercise. In the next post, I will introduce you how to write clojure-idiom browser script!