Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Working idiomatically through tutorial14.js

  • Loading branch information...
commit 1bc56be42d40a696ff426cb5c5678a5cea1187b4 1 parent 329cc94
Josh Lehman authored
1  .gitignore
View
@@ -6,4 +6,5 @@ pom.xml
/out/
/target/
*.js
+.nrepl-port
.lein-deps-sum
8 app.js
View
@@ -9,10 +9,14 @@ goog.addDependency("../cljs/core/async/impl/ioc_helpers.js", ['cljs.core.async.i
goog.addDependency("../cljs/core/async/impl/channels.js", ['cljs.core.async.impl.channels'], ['cljs.core.async.impl.buffers', 'cljs.core', 'cljs.core.async.impl.dispatch', 'cljs.core.async.impl.protocols']);
goog.addDependency("../cljs/core/async/impl/timers.js", ['cljs.core.async.impl.timers'], ['cljs.core', 'cljs.core.async.impl.channels', 'cljs.core.async.impl.dispatch', 'cljs.core.async.impl.protocols']);
goog.addDependency("../cljs/core/async.js", ['cljs.core.async'], ['cljs.core.async.impl.ioc_helpers', 'cljs.core.async.impl.buffers', 'cljs.core', 'cljs.core.async.impl.channels', 'cljs.core.async.impl.dispatch', 'cljs.core.async.impl.protocols', 'cljs.core.async.impl.timers']);
+goog.addDependency("../clojure/string.js", ['clojure.string'], ['cljs.core', 'goog.string', 'goog.string.StringBuffer']);
+goog.addDependency("../no/en/core.js", ['no.en.core'], ['cljs.core', 'clojure.string', 'goog.crypt.base64', 'cljs.reader']);
+goog.addDependency("../cljs_http/util.js", ['cljs_http.util'], ['goog.userAgent', 'cljs.core', 'no.en.core', 'clojure.string', 'goog.json', 'goog.Uri']);
+goog.addDependency("../cljs_http/core.js", ['cljs_http.core'], ['cljs.core', 'cljs_http.util', 'goog.net.XhrIo', 'cljs.core.async']);
goog.addDependency("../om/dom.js", ['om.dom'], ['cljs.core']);
goog.addDependency("../om/core.js", ['om.core'], ['cljs.core', 'om.dom']);
-goog.addDependency("../clojure/string.js", ['clojure.string'], ['cljs.core', 'goog.string', 'goog.string.StringBuffer']);
+goog.addDependency("../cljs_http/client.js", ['cljs_http.client'], ['cljs.core', 'no.en.core', 'cljs_http.core', 'cljs_http.util', 'clojure.string', 'cljs.reader', 'cljs.core.async', 'goog.Uri']);
goog.addDependency("../secretary/core.js", ['secretary.core'], ['cljs.core', 'clojure.string']);
goog.addDependency("../markdown/transformers.js", ['markdown.transformers'], ['cljs.core', 'clojure.string']);
goog.addDependency("../markdown/core.js", ['markdown.core'], ['cljs.core', 'markdown.transformers']);
-goog.addDependency("../react_tutorial_om/app.js", ['react_tutorial_om.app'], ['goog.history.EventType', 'cljs.core', 'secretary.core', 'om.core', 'goog.History', 'react_tutorial_om.utils', 'markdown.core', 'om.dom', 'cljs.core.async', 'goog.events']);
+goog.addDependency("../react_tutorial_om/app.js", ['react_tutorial_om.app'], ['goog.history.EventType', 'cljs.core', 'cljs_http.client', 'secretary.core', 'om.core', 'goog.History', 'react_tutorial_om.utils', 'markdown.core', 'om.dom', 'cljs.core.async', 'goog.events']);
4 comments.json
View
@@ -0,0 +1,4 @@
+[
+ {"author": "Pete Hunt", "text": "This is one comment"},
+ {"author": "Jordan Walke", "text": "This is *another* comment"}
+]
1  project.clj
View
@@ -7,6 +7,7 @@
[org.clojure/core.async "0.1.267.0-0d7780-alpha"]
[secretary "0.4.0"]
[markdown-clj "0.9.36"]
+ [cljs-http "0.1.2"]
[om "0.1.0-SNAPSHOT"]]
:plugins [[lein-cljsbuild "1.0.0"]]
62 src/react_tutorial_om/app.cljs
View
@@ -2,34 +2,49 @@
(:require-macros [cljs.core.async.macros :refer [go alt!]]
[secretary.macros :refer [defroute]])
(:require [goog.events :as events]
- [cljs.core.async :refer [put! <! chan]]
+ [cljs.core.async :refer [put! <! >! chan timeout]]
[markdown.core :as md]
[om.core :as om :include-macros true]
[om.dom :as dom :include-macros true]
[secretary.core :as secretary]
+ [cljs-http.client :as http]
[react-tutorial-om.utils :refer [guid]])
(:import [goog History]
[goog.history EventType]))
-;; Because swannodette did...
(enable-console-print!)
+;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
+;; Util
+
+(defn- with-id
+ [m]
+ (assoc m :id (guid)))
+
+(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)))))))
+
+;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
+;; Components
+
(def app-state
- (atom {:comments
- [{:author "Pete Hunt" :text "This is one comment." :id (guid)}
- {:author "Jordan Walke" :text "This is *another* comment." :id (guid)}]}))
+ (atom {:comments []}))
(defn comment-form []
(om/component
(dom/div {:className "commentForm"} "Hello, world! I am a commentForm.")))
-;; Is this the right way to do things? In the tutorial they use
-;; {this.props.children} instead of passing the comment as an
-;; argument. How would I go about that?
-
-;; Can't seem to get this to work. Moving on.
-;; (dom/span {:dangerouslySetInnerHTML (js-obj {:__html raw-markup})})
(defn comment
+ "Having some trouble getting the markdown to display properly. I've
+ tried with:
+
+ (dom/span {:dangerouslySetInnerHTML (js-obj {:__html raw-markup})})
+
+ and other variants...but haven't gotten anywhere."
+
[{:keys [author text] :as c} opts]
(om/component
(let [raw-markup (md/mdToHtml text)]
@@ -47,9 +62,26 @@
(range (count (:comments app))))))))
(defn comment-box [app opts]
+ (reify
+ om/IInitState
+ (init-state [_ owner]
+ (om/update! app #(assoc % :comments [])))
+ om/IWillMount
+ (will-mount [_ owner]
+ (go (while true
+ (fetch-comments app opts)
+ (<! (timeout (:poll-interval opts))))))
+ om/IRender
+ (render [_ owner]
+ (dom/div {:className "commentBox"}
+ (dom/h1 nil "Comments")
+ (om/build comment-list app)))))
+
+(defn tutorial-app [app]
(om/component
- (dom/div {:className "commentBox"}
- (dom/h1 nil "Comments")
- (om/build comment-list app))))
+ (dom/div nil
+ (om/build comment-box app
+ {:opts {:poll-interval 2000
+ :url "comments.json"}}))))
-(om/root app-state comment-box (.getElementById js/document "content"))
+(om/root app-state tutorial-app (.getElementById js/document "content"))
Please sign in to comment.
Something went wrong with that request. Please try again.