Instantly share code, notes, and snippets.

@bobo bobo/core.clj
Created Sep 3, 2010

What would you like to do?
(ns SimpleTasks.core
(:use compojure.core
ring.adapter.jetty, ring.middleware.params, ring.middleware.session, ring.middleware.file-info,ring.middleware.file))
(defn display []
(html [:h1 "hello world!"]))
(defn include-all-js []
(include-js "/jquery.js")
(include-js "/javascript.js")
(include-js "/jquery.form.js")))
(defn render-form []
(form-to {:id "myform"} [:post "/form" ]
"Add task"
(text-field :task)
(submit-button "save"))
[:div {:id "formresult"} "form result will be here"]))
(defn handle-form [task session]
(let [tasks (session :tasks)
result (if (set? tasks)
(conj tasks task)
(hash-set task))]
{:body (html (unordered-list result))
:session {:tasks result}}))
(defn view [session]
(html [:h1 "Tasks" ]
(unordered-list (:tasks session))))
(defn delete-task [task session]
{:body (str "removed" task)
:session {:tasks (disj (session :tasks) task)}})
(defroutes myroutes
(GET "/delete/:task" {session :session, params :params} (delete-task (params "task") session))
(GET "/" [] (display))
(GET "/form" [] (render-form))
(GET "/view" {session :session} (view session))
(POST "/form" {session :session, params :params} (handle-form (params "task") session)))
(wrap! myroutes :session)
(def app (->
(wrap-file "files")
(defonce server (run-jetty app
{:join? false
:port 8080}))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment