practicalli / clojure-web-services

Develop production grade server-side web services and APIs using Clojure and REPL driven development
https://practical.li/clojure-web-services
Creative Commons Attribution Share Alike 4.0 International
11 stars 14 forks source link

Question: Web Apps, forms and UI #65

Open practicalli-johnny opened 4 years ago

practicalli-johnny commented 4 years ago

using an MVC architecture in a ring based web app. In a controller function for an edit form, I want to be able to assoc-in multiple bits of data to the request map which include the data for the form itself, data to populate at least one dropdown, and when I get there, validation messaging such as "Name is required".Since the request map isn't mutable, my naive view of a potential solution would be to use a chain of functions to modify the request. Here's what I have now to populate the form with data, depending on if it is a new record or an edit:

(defn edit-nutrient [req] (view/nutrient-form (nutrient-form-data req)))(defn nutrient-form-data [req] (if (nil? (get-in req [:path-params :id])) (assoc-in req [:params :q] [{:eid -1 :name "" :grams-in-stock 0 :purchase-url "" :note ""}]) (assoc-in req [:params :q] (m/find-nutrient (Long/parseLong (get-in req [:path-params :id]))))))

edit-nutrient is the actual controller function and nutrient-form-data a helper function.Now I want to add (assoc-in req [:params :qc] (m/find-all-categories)) to the processing here and feel that this is not a clean approach to passing data into a view, particularly if it requires multiple chunks of data.

Best practice suggestions