Skip to content
Find file
Fetching contributors…
Cannot retrieve contributors at this time
49 lines (42 sloc) 1.44 KB
(ns yolk-examples.client.autocomplete
(:require [jayq.core :refer [$] :as j]
[dommy.template :as template]
[yolk.bacon :as b]
[yolk.ui :as ui]
[ :as net]
[clojure.browser.repl :as repl])
(:use-macros [yolk.macros :only [->log ->logi]]))
(defn throttled-input []
(-> (ui/->stream ($ "#search-input") "keyup")
(b/map #(j/val ($ "#search-input")))
(b/throttle 500)
(b/filter #(> (count %) 2))
(defn search-wikipedia [term]
(net/ajax {:url ""
:data {:action "opensearch"
:search term
:format :json}
:dataType :jsonp}))
(defn suggestions []
(-> (throttled-input)
(b/flat-map-latest search-wikipedia)
(b/map #(js->clj % :keywordize-keys true))
(b/filter (fn [data]
(and (:0 data) (:1 data))))))
(defn display-response [$elem]
(fn [data]
(j/empty $elem)
(doseq [t (:1 data)]
(j/append $elem (template/node [:li t])))))
(def content (template/node
[:h2 "Search Wikipedia"]
(defn ^:export main []
(j/append ($ "#main-content") content)
(let [$elem ($ "#results")]
(b/on-value (suggestions)
(display-response $elem)))
(.focus ($ :#search-input)))
Something went wrong with that request. Please try again.