On Tuesday, 3 March 2015 20:20:41 UTC+11, Zubair Quraishi  wrote:
> You would have to use a Clojure macro to abstract this, which in turn will 
> call a paramaterised function
> 
> On Sunday, March 1, 2015 at 4:24:18 PM UTC+1, Sven Richter wrote:
> > Hi,
> > 
> > I have several functions that do an async request to the server. There are 
> > constraints for this functions.
> > 
> > 1. Only send an request if some value is not empty
> > 2. Disable and enable some button before and after the request. 
> > 
> > A function might look like this:
> > 
> > (defn add-group []
> >   (let [g-name (h/get-value "add-group-input")
> > ;repetitive get element
> >         clicked-button (h/get-elem "add-group-btn")] 
> > ;repetitive only send when not empty
> >     (when (not-empty g-name)
> >       (go (let [[ok uuid] (<! (h/post-async "/admin/group/add" {:name 
> > g-name} clicked-button))]
> >            (if ok (do (swap! app-state update-in [:groups] conj {:name 
> > g-name :uuid uuid})
> >                       (set-user-selection-hidden-state (:groups @app-state))
> >                       (h/show-success-message "notification-div" "Group 
> > Added"))
> >                   (h/show-error-message "notification-div"))
> > ;repetitive remove disbled attribute
> >            (.removeAttribute clicked-button "disabled"))))))
> > 
> > How would I put this repeating code into a function? Or would I need to 
> > write a macro for this kind of repetition?
> > 
> > Best Regards,
> > Sven

you may use a macro. But it is not essential. You can simplify this by looking 
for chunks of functionality that you want to be able to change out and passing 
these as functions to be called in the right place in your boilerplate. (the 
entire do block after ok for example). Once you do this you will probably see 
other opportunities for simplification.

You can also simplify by ensuring that the side effect specific code is 
separated from the data manipulation code and does nothing except the side 
effect that it needs to complete.

You have at least 2 stateful parts here. the server and the ui. They are both 
entangled in this function. Consider pulling them apart.

Dave




-- 
Note that posts from new members are moderated - please be patient with your 
first post.
--- 
You received this message because you are subscribed to the Google Groups 
"ClojureScript" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/clojurescript.

Reply via email to