Skip to content

Instantly share code, notes, and snippets.

@kriyative
Created January 20, 2011 19:43
Show Gist options
  • Save kriyative/788487 to your computer and use it in GitHub Desktop.
Save kriyative/788487 to your computer and use it in GitHub Desktop.
server side Clojure for setting up routes etc.
;;; -*- Mode: Clojure -*-
;; This is clojurejs code which is translated to javascript and runs in the browser
(defn login-submit! []
(.text ($ ".error") "")
(.text ($ "#login-message") "submitting ..."))
(defn dialog-close [] (.dialog ($ this) :close))
(defn open-login-dialog []
(when (empty? ($ "#login-dialog"))
(.append ($ document.body)
(html
[:div {:id :login-dialog :title "login"}
[:form {:id :login-form}
[:label {:for :email} "Email:"]
[:input {:type :text :id :email :name :email}]
[:label {:for :password} "Password:"]
[:input {:type :password :id :password :name :password}]]
[:p {:id :login-message :class "message error"} ""]]))
(.dialog ($ "#login-dialog")
{:autoOpen false
:modal true
:width 300
:height 350
:resizable false
:open (fn [e ui] (.text ($ ".error") ""))
:buttons {:login login-submit! :close dialog-close}}))
(.dialog ($ "#login-dialog") :open))
;;; this is Clojure code running on the server side, setting up the routes and
;;; serving the dynamically compiled JavaScript
(ns myapp.servlet
(:use clojurejs.js)
(:use compojure.core
compojure.route
hiccup.page-helpers
hiccup.form-helpers
ring.adapter.jetty
hiccup.core))
(defn app-script-js []
{:status 200
:content-type "text/javascript"
:body (with-pretty-print
(tojs
"/path/to/clojurejs/src/clojurejs/boot.cljs"
"/path/to/myapp.clj"))})
(defn index-handler []
(html
[:html
[:head
[:title "myapp"]
(include-css "/resources/jqueryui/1.8.2/themes/smoothness/jquery-ui.css")
(include-js "/resources/jquery/1.4.4/jquery.js"
"/resources/jqueryui/1.8.2/jquery-ui.js"
"/myapp/app.js")]
[:body
[:h1 "welcome to myapp"]
[:a {:href "#" :onclick (js (open-login-dialog))} "login"]]]))
(defroutes myapp-routes
(GET "/myapp" [] (index-handler))
(GET "/myapp/app.js" [] (app-script-js))
(files "/resources/" {:root (path-to "resources")})
(not-found "Page not found"))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment