微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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.

原文地址:https://www.jb51.cc/ajax/160017.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐