Skip to content

Instantly share code, notes, and snippets.

@mprokopov
Created December 10, 2017 19:19
Show Gist options
  • Save mprokopov/193a2a13ccaf5353e59da4484bc68a82 to your computer and use it in GitHub Desktop.
Save mprokopov/193a2a13ccaf5353e59da4484bc68a82 to your computer and use it in GitHub Desktop.
Pure ClojureScript ajax JSON remote call with goog.net.XhrIo
(ns test3.main
(:require [goog.dom :as gdom]
[goog.net.XhrIo]
[goog.object :as gobj]))
(defn user-list
"returns HTML string from users list of maps m"
[m]
(str "<ul>"
(apply str
(for [{:keys [name email]} m]
(str "<li>" name "-" email "</li>")))
"</ul>"))
(defn handler
"renders user-list onto #app element, used as callback function"
[e]
(let [target (gobj/get e "target")
users-json (.getResponseJson target)
users (js->clj users-json :keywordize-keys true)
app-element (gdom/getElement "app")]
(gobj/set app-element "innerHTML" (user-list users))))
(goog.net.XhrIo/send "/users.json" handler)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment