Created
December 10, 2017 19:19
-
-
Save mprokopov/193a2a13ccaf5353e59da4484bc68a82 to your computer and use it in GitHub Desktop.
Pure ClojureScript ajax JSON remote call with goog.net.XhrIo
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 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