Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Working server and memory store

  • Loading branch information...
commit 95c46d1c0d0178afb6833d6df045a0d8fc9c0df0 1 parent e1c4f00
@jalehman authored
View
20 project.clj
@@ -3,6 +3,12 @@
:url "http://example.com/FIXME"
:dependencies [[org.clojure/clojure "1.5.1"]
+ [org.clojure/tools.reader "0.8.2"]
+ ;; CLJ
+ [ring/ring-core "1.2.0"]
+ [compojure "1.1.6"]
+ [cheshire "5.2.0"]
+ ;; CLJS
[org.clojure/clojurescript "0.0-2127"]
[org.clojure/core.async "0.1.267.0-0d7780-alpha"]
[secretary "0.4.0"]
@@ -10,7 +16,11 @@
[cljs-http "0.1.2"]
[om "0.1.0-SNAPSHOT"]]
- :plugins [[lein-cljsbuild "1.0.0"]]
+ :plugins [[lein-cljsbuild "1.0.0"]
+ [lein-ring "0.8.7"]]
+
+ :ring {:handler react-tutorial-om.core/app
+ :init react-tutorial-om.core/init}
:source-paths ["src/clj"]
@@ -18,16 +28,16 @@
:builds [{:id "dev"
:source-paths ["src/cljs"]
:compiler {
- :output-to "app.js"
- :output-dir "out"
+ :output-to "resources/public/js/app.js"
+ :output-dir "resources/public/js/out"
:optimizations :none
:source-map true
:externs ["om/externs/react.js"]}}
{:id "release"
:source-paths ["src/cljs"]
:compiler {
- :output-to "app.js"
- :source-map "app.js.map"
+ :output-to "resources/public/js/app.js"
+ :source-map "resources/public/js/app.js.map"
:optimizations :advanced
:pretty-print false
:output-wrapper false
View
17 resources/public/index.html
@@ -0,0 +1,17 @@
+<!DOCTYPE html>
+<html lang="en">
+ <head>
+ <meta charset="utf-8"></meta>
+ <meta http-equiv="X-UA-Compatible" content="IE=edge">
+ <title>React Tutorial Rewritten in OM</title>
+ </head>
+ <body>
+ <!-- entry point for components -->
+ <div id="content"></div>
+
+ <script src="http://fb.me/react-0.5.1.js"></script>
+ <script src="js/out/goog/base.js" type="text/javascript"></script>
+ <script src="js/app.js" type="text/javascript"></script>
+ <script type="text/javascript">goog.require("react_tutorial_om.app");</script>
+ </body>
+</html>
View
42 src/clj/react_tutorial_om/core.clj
@@ -0,0 +1,42 @@
+(ns react-tutorial-om.core
+ (:require [compojure.handler :as handler]
+ [compojure.route :as route]
+ [compojure.core :refer [GET POST defroutes]]
+ [ring.util.response :as resp]
+ [cheshire.core :as json]
+ [clojure.java.io :as io]))
+
+(def comments (atom []))
+
+(defn json-response [data & [status]]
+ {:status (or status 200)
+ :headers {"Content-Type" "application/json"}
+ :body (json/generate-string data)})
+
+(defn init
+ []
+ (reset! comments (-> (slurp "comments.json")
+ (json/parse-string true)
+ vec)))
+
+(defn save-comment!
+ [{:keys [body]}]
+ (let [comment (-> body io/reader slurp (json/parse-string true))]
+ (swap! comments conj comment)
+ (json-response
+ {:message "Saved comment!"})))
+
+(defroutes app-routes
+ (GET "/" [] (resp/redirect "/index.html"))
+
+ (GET "/comments" [] (json-response
+ {:message "Here's the comments!"
+ :comments @comments}))
+ (POST "/comments" req (save-comment! req))
+
+ (route/resources "/")
+ (route/not-found "Page not found"))
+
+(def app
+ (-> #'app-routes
+ (handler/api)))
View
18 src/cljs/react_tutorial_om/app.cljs
@@ -24,8 +24,9 @@
(defn- fetch-comments
"The comments need to be a vector, not a list. Not sure why."
[app opts]
- (go (let [{body :body} (<! (http/get (:url opts)))]
- (om/update! app #(assoc % :comments (vec (map with-id body)))))))
+ (go (let [{{cs :comments} :body} (<! (http/get (:url opts)))]
+ (om/update!
+ app #(assoc % :comments (vec (map with-id cs)))))))
(defn- value-from-node
[owner field]
@@ -72,9 +73,8 @@
[comment app opts]
(do (om/update! app [:comments]
(fn [comments] (conj comments (assoc comment :id (guid)))))
- (go (let [res (<! (http/post (:url opts)
- {:body comment}))]
- (prn res)))))
+ (go (let [res (<! (http/post (:url opts) {:json-params comment}))]
+ (prn (:message res))))))
(defn handle-submit
[e app owner opts]
@@ -116,17 +116,11 @@
(defn tutorial-app [app]
(reify
- ;; om/IWillMount
- ;; (will-mount [_ owner]
- ;; (let [events (chan)]
- ;; (om/set-state! owner [:events] events)
- ;; (go (while true
- ;; (handle-event app (<! events))))))
om/IRender
(render [_ owner]
(dom/div nil
(om/build comment-box app
{:opts {:poll-interval 2000
- :url "comments.json"}})))))
+ :url "/comments"}})))))
(om/root app-state tutorial-app (.getElementById js/document "content"))
Please sign in to comment.
Something went wrong with that request. Please try again.