Skip to content

Instantly share code, notes, and snippets.

@speedsticko
Created December 20, 2013 16:23
Show Gist options
  • Save speedsticko/8057258 to your computer and use it in GitHub Desktop.
Save speedsticko/8057258 to your computer and use it in GitHub Desktop.
(ns async-tut1.core
(:require-macros [cljs.core.async.macros :refer [go]])
(:require [goog.dom :as dom]
[goog.events :as events]
[cljs.core.async :refer [put! chan <!]])
(:import [goog.net Jsonp]
[goog Uri]))
(.log js/console (dom/getElement "query"))
(defn listen [el type]
(let [out (chan)]
(events/listen el type
(fn [e] (put! out e)))
out))
(let [clicks (listen (dom/getElement "search") "click")]
(go (while true
(.log js/console (<! clicks)))))
(def wiki-search-url
"http://en.wikipedia.org/w/api.php?action=opensearch&format=json&search=")
(defn jsonp [uri]
(let [out (chan)
req (Jsonp. (Uri. uri))]
(.send req nil (fn [res] (put! out res)))
out))
(defn query-url [q]
(str wiki-search-url q))
(comment go (.log js/console (<! (jsonp (query-url "cats")))))
(defn user-query []
(.-value (dom/getElement "query")))
(defn render-query [results]
(str
"<ul>"
(apply str
(for [result results]
(str "<li>" result "</li>")))
"</ul>"))
(defn init []
(let [clicks (listen (dom/getElement "search") "click")
results-view (dom/getElement "results")]
(go (while true
(<! clicks)
(let [[_ results] (<! (jsonp (query-url (user-query))))]
(set! (.-innerHTML results-view) (render-query results)))))))
(init)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment