There’s already an AJAX re-frame effect handler provided by Day8, the same guy who made re-frame and there’s nothing wrong with it. From the documentation, this is how you use it:

(re-frame/reg-event-fx ::http-post
  (fn [_world [_ val]]
    {:http-xhrio {:method          :post
                  :uri             "https://httpbin.org/post"
                  :params          data
                  :format          (ajax/json-request-format)
                  :response-format (ajax/json-response-format {:keywords? true})
                  :on-success      [::good-post-result]
                  :on-failure      [::bad-post-result]}}))

That felt a little to verbose for my taste, so I made my own that you use like this:


(re-frame/reg-event-fx ::http-post
  (fn [_world [_ val]]
    {:ajax {:post            "https://httpbin.org/post"
            :params          data
            :on-success      [::good-post-result]
            :on-failure      [::bad-post-result]}}))

If you are familiar with cljs-ajax, you’ll notice the pattern. Day8’s solution uses the raw cljs-ajax API, while I use the higher level one. Day8’s is more versatile, mine is more compact.

This is my solution:

(ns ajax-fx-handler
  (:require [ajax.core :as ajax]
            [re-frame.core :as re-frame]
            [clojure.set :as set]))

(re-frame/reg-fx :ajax
  (fn http-effect [request]
    (let [request-verb (set/intersection #{:get :post :put :patch :delete :head :options :trace :purge}
                                         (set (keys request)))]
      (if (not= 1 (count request-verb))
        (throw (js/Error (str "When specifying an AJAX request, one and only one verb should be specified. Found: " request-verb)))
        (let [request-verb (first request-verb)
              url (get request request-verb)
              request (if (contains? request :on-success)
                        (assoc request :handler #(re-frame/dispatch (conj (:on-success request) %)))
                        request)
              request (if (contains? request :on-failure)
                        (assoc request :error-handler #(re-frame/dispatch (conj (:on-failure request) %)))
                        request)
              request (-> request
                          (dissoc request-verb)
                          (dissoc :on-success)
                          (dissoc :on-failure))
              ajax-fn (cond
                        (= request-verb :get) ajax/GET
                        (= request-verb :post) ajax/POST
                        (= request-verb :put) ajax/PUT
                        (= request-verb :patch) ajax/PATCH
                        (= request-verb :delete) ajax/DELETE
                        (= request-verb :head) ajax/HEAD
                        (= request-verb :options) ajax/OPTIONS
                        (= request-verb :trace) ajax/TRACE
                        (= request-verb :purge) ajax/PURGE)]
          (ajax-fn url request))))))

Feel free to use it, but, is it worth releasing it as a library? does anybody want that?

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s