-
Notifications
You must be signed in to change notification settings - Fork 1
Open
Description
It wasn't obvious to me how to write code like (post-command :set-color :id 55 :color (.. evt -target -value)) and get an expression like "@post(appendQueryString('/cmd/set-color', ':id', 55, ':color', evt.target.value))", because even if I wrap the js part in ->expr, it's already a string by the time post-command gets it. It seems like treating these expressions as objects rather than strings might make more sense for cases like this. Should the library have something like this?
(defprotocol IJSExpression
(form [this] "Get clojure form of expression")
(js [this] "Get js form of expression"))
(deftype JSExpression [form]
Object
(toString [this] (js this))
IJSExpression
(form [this] form)
(js [this] (->expr ~form)))
(defmacro js-expr [form]
`(->JSExpression (quote ~form)))
(comment
(js-expr (+ 5 6)) ;; #object[kc.admin.server.datastar.JSExpression 0x2b3781b9 "(5) + (6)"]
(str (js-expr (+ 5 6))) ;; "(5) + (6)"
(js (js-expr (+ 5 6))) ;; "(5) + (6)"
(form (js-expr (+ 5 6))) ;; (+ 5 6)
)
;; And to demonstrate my use case:
(defn post-command [cmd & params]
(let [params (map (fn [x]
(if (instance? JSExpression x)
(form x)
(pr-str x)))
params)]
(->expr (@post (appendQueryString ~(str "/cmd/" cmd) ~@params)))))
(comment
(post-command :set-color) ;; "@post(appendQueryString(\"/cmd/:set-color\"))"
(post-command :set-color :id 55 :color (js-expr (.. evt -target -value)))
;; "@post(appendQueryString(\"/cmd/:set-color\", \":id\", \"55\", \":color\", evt.target.value))"
)Ramblurr
Metadata
Metadata
Assignees
Labels
No labels