Skip to content

Instantly share code, notes, and snippets.

@dasibre
Created August 5, 2016 19:59
Show Gist options
  • Select an option

  • Save dasibre/b8c09221b62e61c9aa76ce4dc85134a0 to your computer and use it in GitHub Desktop.

Select an option

Save dasibre/b8c09221b62e61c9aa76ce4dc85134a0 to your computer and use it in GitHub Desktop.
learning cljs
[re-frame.core :as re-frame]
[ajax.core :as ajax]
[day8.re-frame.http-fx :as reg-fx]
(defn newpage [ratom]
(println "new page")
[:div
[:h2 (:api-result ratom)]])
(defn page [ratom]
(let [on-click (fn [] (re-frame/dispatch [:handler-with-http app-state]) (reagent/render [newpage app-state]
(.getElementById js/document "app")))]
(fn []
[:button.btn.btn-default {:type "button" :on-click on-click}
"my test"])))
(re-frame/reg-event-fx ;; note the trailing -fx
:handler-with-http ;; usage: (dispatch [:handler-with-http])
(fn [{:keys [db]} _] ;; the first param will be "world"
{:db (assoc db :show-twirly true) ;; causes the twirly-waiting-dialog to show??
:http-xhrio {:method :get
:uri "https://api.github.com/orgs/day8"
:timeout 8000 ;; optional see API docs
:response-format (ajax/json-response-format {:keywords? true}) ;; optional see API docs
:on-success [:good-http-result]
:on-failure [:bad-http-result]}}))
(re-frame/reg-event
:bad-http-result
(fn [app-state [_ result]]
(assoc app-state :api-result result)))
(re-frame/reg-event
:good-http-result
(fn [db [_ result]]
(println "new event triggered")
(println db)
(assoc db :api-result result)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment