在Re-frame中安装初始Ajax数据

时间:2018-09-28 23:48:32

标签: clojurescript 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))))))

0 个答案:

没有答案