Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
(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 (vector? tasks)
(conj tasks task)
(vector task))]
{:body (html (unordered-list result))
:session {:tasks result}}))
(defn view [session]
(html [:h1 "Tasks" ]
(unordered-list (:tasks session))))
(defroutes myroutes
(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