Skip to content

Instantly share code, notes, and snippets.

@bobo
Created September 3, 2010 12:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bobo/563831 to your computer and use it in GitHub Desktop.
Save bobo/563831 to your computer and use it in GitHub Desktop.
(ns SimpleTasks.core
(:use compojure.core
hiccup.core,hiccup.form-helpers, hiccup.page-helpers
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 []
(html
(include-js "/jquery.js")
(include-js "/javascript.js")
(include-js "/jquery.form.js")))
(defn render-form []
(html
(include-all-js)
(form-to {:id "myform"} [:post "/form" ]
"Add task"
[:br]
(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 (->
#'myroutes
(wrap-file "files")
wrap-file-info))
(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