From 0f7208b31f7bf77e1874cdbb757a2d6a0a2ad327 Mon Sep 17 00:00:00 2001 From: awb99 Date: Thu, 7 Sep 2023 14:44:56 -0500 Subject: [PATCH] ajax promisify bugfix --- README.md | 4 +-- demo/demo/page5/http.cljs | 14 +++++++++- deps.edn | 12 +++++--- resources/ext/ajax.edn | 3 +- src/pinkgorilla/repl/cljs/ajax.cljs | 43 +++++++++++++++++------------ 5 files changed, 51 insertions(+), 25 deletions(-) diff --git a/README.md b/README.md index cf2cc4b..f1db2d3 100644 --- a/README.md +++ b/README.md @@ -20,8 +20,8 @@ clj -X:goldly To compile js and run: ``` -clj -X:goldly:build :profile "npm-install" -clj -X:goldly:build :profile "compile2" +clj -X:goldly:build:npm-install +clj -X:goldly:build:compile clj -X:goldly ``` diff --git a/demo/demo/page5/http.cljs b/demo/demo/page5/http.cljs index f4b629b..706bc0d 100644 --- a/demo/demo/page5/http.cljs +++ b/demo/demo/page5/http.cljs @@ -2,7 +2,16 @@ (:require [goldly.page :as page] [reagent.core :as r] - [http :refer [get-edn get-str get-json]])) + [http :refer [get-edn get-str get-json]] + [promesa.core :as p] + [ajax.core :refer [GET]] + )) + +(defn ajax-get [state url] + (let [rp (GET url)] + (p/then rp (fn [data] + (swap! state assoc :ajax data) + )))) (defn http-get-page [{:keys [_route-params _query-params _handler] :as _route}] (let [state (r/atom {:edn nil @@ -19,6 +28,9 @@ [:input.m-2 {:type "button" :value "get json" :on-click #(get-json "/r/repl/bongo.json" state [:json])}] + [:input.m-2 {:type "button" + :value "get text (via ajax GET)" + :on-click #(ajax-get state "/r/repl/bongo.txt")}] [:p "state: " (pr-str @state)]]))) (page/add http-get-page :demo-http-get) \ No newline at end of file diff --git a/deps.edn b/deps.edn index edf44df..1d8fb10 100644 --- a/deps.edn +++ b/deps.edn @@ -61,21 +61,25 @@ {:extra-paths ["target/webly" ; to include the sci-bindings info "node_modules" "demo"] - :extra-deps {org.pinkgorilla/goldly {:mvn/version "0.4.592" + :extra-deps {org.pinkgorilla/goldly {:mvn/version "0.4.611" :exclusions [org.pinkgorilla/ui-repl]} - org.pinkgorilla/devtools {:mvn/version "0.0.2" + org.pinkgorilla/devtools {:mvn/version "0.0.10" :exclusions [org.pinkgorilla/ui-repl]} - org.pinkgorilla/ui-bidi {:mvn/version "0.0.14"} + org.pinkgorilla/ui-bidi {:mvn/version "0.0.15"} } :exec-fn modular.system/start! :exec-args {:profile "jetty" :services "goldly/services.edn" - :config ["webly/config.edn" "goldly/config.edn" "goldly/docs-config.edn" "repl-config.edn"]}} + :config ["webly/config.edn" "goldly/config.edn" "repl-config.edn"]}} :build {:exec-fn goldly.app.build/goldly-build :exec-args {:profile "npm-install"}} + :npm-install + {:exec-fn goldly.app.build/goldly-build + :exec-args {:profile "npm-install"}} + :compile {:exec-fn goldly.app.build/goldly-build :exec-args {:profile "compile2"}} diff --git a/resources/ext/ajax.edn b/resources/ext/ajax.edn index 94c912d..fa0d7d4 100644 --- a/resources/ext/ajax.edn +++ b/resources/ext/ajax.edn @@ -1,7 +1,8 @@ {:name "cljs-ajax" ; build :lazy false - :cljs-namespace [pinkgorilla.repl.cljs.ajax] + :cljs-namespace [ajax.core + pinkgorilla.repl.cljs.ajax] :cljs-ns-bindings {'ajax.core {'GET pinkgorilla.repl.cljs.ajax/http-get 'POST pinkgorilla.repl.cljs.ajax/http-post 'PUT pinkgorilla.repl.cljs.ajax/http-put diff --git a/src/pinkgorilla/repl/cljs/ajax.cljs b/src/pinkgorilla/repl/cljs/ajax.cljs index f5ab06a..077b3f9 100644 --- a/src/pinkgorilla/repl/cljs/ajax.cljs +++ b/src/pinkgorilla/repl/cljs/ajax.cljs @@ -5,23 +5,32 @@ [ajax.core :refer [GET POST PUT DELETE]] [clojure.string :as str])) -(defn make-promise [AJAX-TYPE] - ([url] - (make-promise url {})) - ([url params] - (p/create - (fn [resolve reject] - (AJAX-TYPE url - (merge params - {:handler (fn [response] - (resolve response)) - :error-handler (fn [error] - (reject error))})))))) - -(def http-get (make-promise GET)) -(def http-post (make-promise POST)) -(def http-put (make-promise PUT)) -(def http-delete (make-promise DELETE)) +(defn wrap-promise + [AJAX-TYPE url params] + (p/create + (fn [resolve reject] + (AJAX-TYPE url + (merge params + {:handler (fn [response] + (resolve response)) + :error-handler (fn [error] + (reject error))}))))) + +(defn http-get + ([url] (http-get url {})) + ([url params] (wrap-promise GET url params))) + +(defn http-post + ([url] (http-post url {})) + ([url params] (wrap-promise POST url params))) + +(defn http-put + ([url] (http-put url {})) + ([url params] (wrap-promise PUT url params))) + +(defn http-delete + ([url] (http-delete url {})) + ([url params] (wrap-promise DELETE url params)))