我在 re-frame 文档中看到了 Ajax 调用的示例:
(reg-event-fx ;; <-- note the `-fx` extension
:request-it ;; <-- the event id
(fn ;; <-- the handler function
[{db :db} _] ;; <-- 1st argument is coeffect, from which we extract db
;; we return a map of (side) effects
{:http-xhrio {:method :get
:uri "http://json.my-endpoint.com/blah"
:format (ajax/json-request-format)
:response-format (ajax/json-response-format {:keywords? true})
:on-success [:process-response]
:on-failure [:bad-response]}
:db (assoc db :loading? true)}))
我可以在主函数中调用事件吗:
(reframe/dispatch-sync [:request-it])
加载初始值?我需要加载初始值,然后渲染视图。
更新
我使用这个功能做到了:
(reframe/reg-event-db
:process-response
(fn
[db [_ response]]
(-> db
(assoc :loading? false) ;; take away that "Loading ..." UI
(assoc :test (js->clj response))
(assoc :questions (js->clj (:questions response))))))