Skip to content

Commit

Permalink
ajax promisify bugfix
Browse files Browse the repository at this point in the history
  • Loading branch information
awb99 committed Sep 7, 2023
1 parent a3fce75 commit 0f7208b
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 25 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
```

Expand Down
14 changes: 13 additions & 1 deletion demo/demo/page5/http.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
12 changes: 8 additions & 4 deletions deps.edn
Original file line number Diff line number Diff line change
Expand Up @@ -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"}}
Expand Down
3 changes: 2 additions & 1 deletion resources/ext/ajax.edn
Original file line number Diff line number Diff line change
@@ -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
Expand Down
43 changes: 26 additions & 17 deletions src/pinkgorilla/repl/cljs/ajax.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -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)))



Expand Down

0 comments on commit 0f7208b

Please sign in to comment.