How should javascript be integrated into a Clojure/Ring web app?
How are Javascript resources best integrated into a ring application?
Specifical开发者_JS百科ly:
- Where would we put our .js files?
- How and where should unit tests be set up?
- What is the best way to set up acceptance tests for functionality that cuts across the client and server sides?
Are there any best practices for javascript with ring applications? One possible answer would be to develop the client and server sides completely separately, to essentially separate everything into two separate projects, but I'm not completely happy with that idea.
(I'm also aware of clojurescript, though I'm thinking mainly of javscript code that's been written as javascript.)
Ring has support to serve files directly from a folder (ring.middleware.file
or ring.middleware.static
which is what I would use) or from a resource in the jar/war. Your best bet is to use these mechanisms to serve your static (javascript/images) content. If you have the freedom to do this, I will put in a route similar to this one to serve all HTTP requests at /static/
from such a folder:
(def *route*
(ring/wrap-static "c:/statics/" ["/static/"]))
Once you know how to handle a request for a static resource (like a javascript resource) it's the same as with anything else like PHP or ASP.
Another alternative is to define your resource routes as per normal, and then in the "catch-all" (normally something like this (ring/GET * request (handle-static-request request))
route, handle any remaining unserviced request with a static request.
To get started quickly, probably the easiest way to get started serving static files in a clojure/ring development environment is to use ring.util.response/file-response.
If you use moustache you could add a route like this to the end of your route list (as a fall-through route):
[&] (fn [req] (file-response (req :uri) {:root "resources/public"}))
It will pick any route and try to find a file that matches the supplied uri under the "[proj-root]/resources/public" directory.
In your html script tag you could just use, for example, src="/js/all.js"
, and then ensure that you have a file like this "[proj-root]/resources/public/js/all.js".
see http://mmcgrana.github.com/ring/ring.util.response.html
I would choose the "complete separation of concerns" path. Your server side clojure code will only expose json services and your javascript will query the data and append it to the html. Clojure makes it very easy for you to create such services and translate it's data structures to json. Of couse this approach has it's downsides, you'll heavily depend on javascript.
I'm not a clojure web expert but I'll give it a shot.
I think it has nothing todo with Ring its about the templating language you use.
Template Languages
- Enlive: Proberly seperat everything (not into a sepred project) and pull it together with enlive
- Fleet: Just how you would do it in PHP or something.
- Hiccup: Write your clojure functions with inline html and js only have big js librarys in sepret files.
I can't really talk about testing.
精彩评论