CloServe A view first web framework in Clojure

Generate Ajax Form with Code Snippet

Generate Ajax Form with Snippet Example

In some application scenerios, one need to generate Ajax form dynamically in response of user action, for eg, user accepted some chat invitation. In this case, we can use form.ajax callback to generate and send another ajax form to the browser, as demonstrated in this example.

Here is the form.ajax call back function that generate another ajax form:

(register-cmd-proc-fn 
 "ChatConfirm"
 [context cmd]
 (let [chatform {:type :element :tag :form
                 :attrs {:class "input-group lift:form.ajax?callback=ChatInput"}
                 :content [{:type :element :tag :input
                            :attrs {:type "text" :name "chatmsg" :class "form-control"}}
                           {:type :element :tag :span
                            :attrs {:class "input-group-btn"}
                            :content [{:type :element :tag :input
                                       :attrs {:value "GO!" :class "btn btn-default" :type "submit"}}]}]}
       newnode (process-snippets chatform (:req context) (:page-id context))
       htmltxt (hickory-to-html newnode)]
   (send-cmd-to-page (:page-id context)
                     {:act :replaceWith
                      :selector (str "#" (:form-id context))
                      :html htmltxt
                      }))
 )

The corresponding HTML code:

<ul class="lift:comet?type=chat"></ul>

<form class="input-group lift:form.ajax?callback=ChatConfirm">
      <input name="ppp" type="hidden" class="">
      <span class="input-group-btn">
        <input value="Join Chat!" class="btn btn-default" type="submit">
      </span>
</form>

Try it here: