Skip to content

reusle/form-juice

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 

Repository files navigation

form-juice

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.

Getting the juice and related stuff


Add the following to the :dependencies vector of your project.clj file:

[clojars version] (https://clojars.org/form-juice)

I'm thirsty!


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"}

Why not join this list of limonades made from form-juice?


kanasubs.com — Convert raw subtitles in Kanji to Kana or Romaji online.

Random license


Copyright (C) 2014 Carlos C. Fontes.

Licensed under the Apache License, Version 2.0.

About

Data squeezed from juicy forms in ClojureScript

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Clojure 100.0%