Data squeezed from juicy forms in ClojureScript. The juice is extracted from real oranges only, with no jQuery additives! terjesb gently provided all the code through this gist. The gist description says:
How to get form data in a ClojureScript app, other than getting values separately? I have a page with many instances of the same form, and a button to submit each form. I couldn't find anything obvious in Domina, Enfocus or Dommy. Then I found goog.dom.forms.getFormDataMap, but the goog.structs.Map it returns doesn't seem to be directly usable from ClojureScript. I did find some work by chouser that uses extend-type on that Map to make it like a regular map, but it still has some arrays in it. goog.dom.forms.getFormDataString returns a x-www-url-encoded string of the form data. I then ported the form-decode from ring.util.codec, and it seems to work very well - returning the form data in a map.
Add the following to the :dependencies
vector of your project.clj
file:
[]
(https://clojars.org/form-juice)
Lets show an example:
(ns app.views.something
(:require [hiccup.def :refer [defelem]]))
(defelem some-form []
[:form {:method "post" :action "/resource"}
; you must name the input var 'event' below
[:button {:type "submit" :onclick "app.something.post(event)"}
"Submit"]])
Add [cljs-ajax "0.2.6"]
to the :dependencies
vector of your
project.clj
file.
(ns app.something
(:require form.juice
[ajax.core :refer [POST]]))
(defn post [event]
(.preventDefault event) ; prevents default event of form submission to fire
(POST "/resource"
{:params (form.juice/squeeze event) ; tasty!
:handler (fn [_] (.log js/console "(*^▽^)/"))
:error-handler (fn [_] (.log js/console "。゚(゚´Д`゚)゚。"))
:format :raw}))
How the juice looks like at the REPL:
(form.juice/squeeze event)
;=> {:orange "juicy", :mandarin "tasty", :lemon "bitter", :grapefruit "gaah"}
kanasubs.com — Convert raw subtitles in Kanji to Kana or Romaji online.
Copyright (C) 2014 Carlos C. Fontes.
Licensed under the Apache License, Version 2.0.