ClojureScript - 在 go 块中有条件地构造 url
ClojureScript - construct url conditionally in go block
我正在使用 cemerick/url 为 ajax 请求构造我的 URL。
但是,查询的某些参数来自异步本机回调。所以我把所有东西都放在 go
块中,如下所示:
(defn myFn [options]
(go (let [options (cond-> options
;; the asynchronous call
(= (:loc options) "getgeo") (assoc :loc (aget (<! (!geolocation)) "coords")))
constructing the url
url (-> "http://example.com"
(assoc-in [:query :param] "a param")
;; How do I not associate those if they don't exist ?
;; I tried something along the lines of this, but it obviously doesn't work.
;; (cond-> (and
;; (-> options :loc :latitude not-nil?)
;; (-> options :loc :latitude not-nil?))
;; (do
;; ))
;; these will fail if there is no "latitude" or "longitude" in options
(assoc-in [:query :lat] (aget (:loc options) "latitude"))
(assoc-in [:query :lng] (aget (:loc options) "longitude"))
;; url function from https://github.com/cemerick/url
(url "/subscribe")
str)])))
我希望能够将 {:loc "local}
或 {:loc {:latitude 12 :longitude 34}}
或 {}
作为参数传递给我的函数。
我觉得我没有使用正确的结构。
我应该如何构造这个 url ?
如果我没看错你的问题,你需要代码,如下所示:
;; helper function
(defn add-loc-to-url-as [url loc k as-k]
(if-let [v (k loc)]
(assoc-in url [:query as-k] v)
url))
;; define function, that process options and extends URL
(defn add-location [url options]
(if-let [location (:loc options)]
(if (string? location)
;;; it expects string, for {:loc "San Francisco"}
(assoc-in url [:query :location] location)
;;; or map, for {:loc {:latitude 12.123, :longitude 34.33}}
(-> url
(add-loc-to-url-as location :latitude :lat)
(add-loc-to-url-as location :longitude :lng)))
url))
;; now you can use it in your block
;; ...
the-url (-> (url "http://example.com" "subscribe")
(assoc-in [:query :param] "a param")
(add-location options))
我正在使用 cemerick/url 为 ajax 请求构造我的 URL。
但是,查询的某些参数来自异步本机回调。所以我把所有东西都放在 go
块中,如下所示:
(defn myFn [options]
(go (let [options (cond-> options
;; the asynchronous call
(= (:loc options) "getgeo") (assoc :loc (aget (<! (!geolocation)) "coords")))
constructing the url
url (-> "http://example.com"
(assoc-in [:query :param] "a param")
;; How do I not associate those if they don't exist ?
;; I tried something along the lines of this, but it obviously doesn't work.
;; (cond-> (and
;; (-> options :loc :latitude not-nil?)
;; (-> options :loc :latitude not-nil?))
;; (do
;; ))
;; these will fail if there is no "latitude" or "longitude" in options
(assoc-in [:query :lat] (aget (:loc options) "latitude"))
(assoc-in [:query :lng] (aget (:loc options) "longitude"))
;; url function from https://github.com/cemerick/url
(url "/subscribe")
str)])))
我希望能够将 {:loc "local}
或 {:loc {:latitude 12 :longitude 34}}
或 {}
作为参数传递给我的函数。
我觉得我没有使用正确的结构。
我应该如何构造这个 url ?
如果我没看错你的问题,你需要代码,如下所示:
;; helper function
(defn add-loc-to-url-as [url loc k as-k]
(if-let [v (k loc)]
(assoc-in url [:query as-k] v)
url))
;; define function, that process options and extends URL
(defn add-location [url options]
(if-let [location (:loc options)]
(if (string? location)
;;; it expects string, for {:loc "San Francisco"}
(assoc-in url [:query :location] location)
;;; or map, for {:loc {:latitude 12.123, :longitude 34.33}}
(-> url
(add-loc-to-url-as location :latitude :lat)
(add-loc-to-url-as location :longitude :lng)))
url))
;; now you can use it in your block
;; ...
the-url (-> (url "http://example.com" "subscribe")
(assoc-in [:query :param] "a param")
(add-location options))