Created
August 3, 2011 16:31
-
-
Save ibdknox/1123079 to your computer and use it in GitHub Desktop.
noir todos ex
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(ns todos.views.welcome | |
(:require [todos.views.common :as common] | |
[noir.content.pages :as pages] | |
[noir.response :as resp]) | |
(:use noir.core | |
hiccup.core | |
hiccup.form-helpers | |
hiccup.page-helpers)) | |
(defonce todos (atom {1 "Get milk" | |
2 "Pay bills" | |
3 "Do stuff"})) | |
(defn add-todo [text] | |
(let [neue-key (inc (apply max (keys @todos)))] | |
(swap! todos assoc neue-key text))) | |
(defn remove-todo [id] | |
(swap! todos dissoc (Integer. id))) | |
(defn valid? [todoText] | |
true) | |
(defpartial todo-item [[id item]] | |
[:li (link-to (str "/todos/remove/" id) item)]) | |
(defpartial todo-form [] | |
(form-to {:id "todoForm"} [:post "/todos"] | |
(label "todoText" "Todo: ") | |
(text-field "todoText") | |
(submit-button {:class "submit"} "add todo"))) | |
(defpage "/todos" [] | |
(common/layout | |
(todo-form) | |
[:ul | |
(map todo-item @todos)])) | |
(defpage [:post "/todos"] {:keys [todoText] :as todo} | |
(if (valid? todoText) | |
(do | |
(add-todo todoText) | |
(render "/todos")) | |
(render "/todos" todo))) | |
(defpage "/todos/remove/:id" {:keys [id]} | |
(swap! todos dissoc (Integer. id)) | |
(resp/redirect "/todos")) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment