Ajax 使用试剂获取
Ajax GET with Reagent
我正在从我的 Reagent 应用程序中执行 Ajax GET,以从数据库加载一些内容。
我不完全确定获得这种 ajax 调用我的页面的结果的最佳方式是什么,考虑到如果我把它放在一个原子中,然后 Reagent 会自动重新呈现一个组件一个原子被取消引用,这意味着我得到了一个无限的 ajax 调用序列。
对于某些代码,
(def matches (atom nil))
(defn render-matches [ms]
(reset! matches (into [:ul] (map (fn [m] ^{:key m}[:li m])
(walk/keywordize-keys (t/read (t/reader :json) ms)))))
这个函数基本上创建了一个[:ul [:li "Stuff here"] [:li "And here"]]
我想在我的页面上显示,现在有以下代码。
(defn standings-page []
(GET "/list-matches"
{:handler render-matches})
@matches)
我认为最好只在一个原子中保存数据并生成 HTML 作为组件逻辑的一部分。
此外,最好在渲染阶段之外触发 AJAX 调用,例如,在组件安装之前,或作为事件的结果(例如单击按钮)。
像这样:
(def matches (atom nil))
(defn component []
(let [get-stuff (fn [] (GET "/..." :handler (fn [response]
(reset! matches (:body response))))]
(get-stuff) <-- called before component mount
(fn []
[:ul
(for [m match]
^{:key ...}
[:li ...])])))
这在 this post 中称为 form-2。
我正在从我的 Reagent 应用程序中执行 Ajax GET,以从数据库加载一些内容。
我不完全确定获得这种 ajax 调用我的页面的结果的最佳方式是什么,考虑到如果我把它放在一个原子中,然后 Reagent 会自动重新呈现一个组件一个原子被取消引用,这意味着我得到了一个无限的 ajax 调用序列。
对于某些代码,
(def matches (atom nil))
(defn render-matches [ms]
(reset! matches (into [:ul] (map (fn [m] ^{:key m}[:li m])
(walk/keywordize-keys (t/read (t/reader :json) ms)))))
这个函数基本上创建了一个[:ul [:li "Stuff here"] [:li "And here"]]
我想在我的页面上显示,现在有以下代码。
(defn standings-page []
(GET "/list-matches"
{:handler render-matches})
@matches)
我认为最好只在一个原子中保存数据并生成 HTML 作为组件逻辑的一部分。
此外,最好在渲染阶段之外触发 AJAX 调用,例如,在组件安装之前,或作为事件的结果(例如单击按钮)。
像这样:
(def matches (atom nil))
(defn component []
(let [get-stuff (fn [] (GET "/..." :handler (fn [response]
(reset! matches (:body response))))]
(get-stuff) <-- called before component mount
(fn []
[:ul
(for [m match]
^{:key ...}
[:li ...])])))
这在 this post 中称为 form-2。