-
-
Save lildata/f8ee56fe0b9f6bcfd93c to your computer and use it in GitHub Desktop.
Following along tutorial here: http://swannodette.github.io/2013/11/07/clojurescript-101/
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 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])) | |
(def wiki-search-url | |
"http://en.wikipedia.org/w/api.php?action=opensearch&format=json&search=") | |
(defn listen [el type] | |
(let [out (chan)] | |
(events/listen el type | |
(fn [e] (put! out e))) | |
out)) | |
(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)) | |
(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