Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

Already on GitHub? Sign in to your account

Add ajaxOnSubmit to SHtml #1414

Closed
wants to merge 2 commits into
from

Conversation

Projects
None yet
3 participants
Owner

Shadowfiend commented Feb 26, 2013

ajaxOnSubmit should be used similarly to onSubmit/onSubmitUnit, to
add proper attributes for AJAX submission to a submit input or button element
without changing the element itself. It should take a ()=>JsCmd like ajaxSubmit
and transform the element it's applied to by adding the right name and onclick
attributes (the same ones that ajaxSubmit puts on the element it generates).

@Shadowfiend Shadowfiend Add ajaxOnSubmit as an AJAX complement to onSubmit.
This is meant to be applied to a button or input type="submit" element,
and adds the attributes needed to correctly run an AJAX form submission
using that button.
f6ea85d
Owner

fmpwizard commented Mar 2, 2013

+1
This one will go in for 2.6 (and 3.0)

@farmdawgnation farmdawgnation commented on the diff Mar 2, 2013

web/webkit/src/main/scala/net/liftweb/http/SHtml.scala
@@ -1580,6 +1580,43 @@ trait SHtml {
}
/**
+ * Add appropriate attributes to an input type="submit" or button
+ * element to make it submit an ajaxForm correctly and return a JsCmd
+ * to the client.
+ *
+ * Example:
+ *
+ * <pre>"type=submit" #> ajaxOnSubmit(() => Alert("Done!"))</pre>
+ */
+ def ajaxOnSubmit(func: () => JsCmd): (NodeSeq)=>NodeSeq = {
+ val fgSnap = S._formGroup.get
+
+ (in: NodeSeq) => S._formGroup.doWith(fgSnap) {
+ def runNodes(ns: NodeSeq): NodeSeq = {
+ def addAttributes(elem: Elem, name: String) = {
+ val clickJs = "liftAjax.lift_uriSuffix = '" + name + "=_'; return true;"
@farmdawgnation

farmdawgnation Mar 2, 2013

Owner

This feels like a magic string that may exist in multiple places in the Lift codebase. Might be worth building a helper function if there isn't one already. Not going to block your PR on it personally, but seemed worth mentioning.

@Shadowfiend

Shadowfiend Apr 14, 2013

Owner

Probably true. May want to wait for a heavier refactoring though.

@farmdawgnation farmdawgnation commented on the diff Mar 2, 2013

web/webkit/src/main/scala/net/liftweb/http/SHtml.scala
@@ -1580,6 +1580,43 @@ trait SHtml {
}
/**
+ * Add appropriate attributes to an input type="submit" or button
+ * element to make it submit an ajaxForm correctly and return a JsCmd
+ * to the client.
@farmdawgnation

farmdawgnation Mar 2, 2013

Owner

Do you tink we should mention in the docs the contrast between this behavior and ajaxSubmit?

@farmdawgnation farmdawgnation commented on the diff Mar 2, 2013

web/webkit/src/main/scala/net/liftweb/http/SHtml.scala
+ def ajaxOnSubmit(func: () => JsCmd): (NodeSeq)=>NodeSeq = {
+ val fgSnap = S._formGroup.get
+
+ (in: NodeSeq) => S._formGroup.doWith(fgSnap) {
+ def runNodes(ns: NodeSeq): NodeSeq = {
+ def addAttributes(elem: Elem, name: String) = {
+ val clickJs = "liftAjax.lift_uriSuffix = '" + name + "=_'; return true;"
+
+ elem % ("name" -> name) % ("onclick" -> clickJs)
+ }
+
+ ns.flatMap {
+ case Group(nodes) => runNodes(nodes)
+
+ case e: Elem if (e.label == "button") ||
+ (e.label == "input" && e.attribute("type").map(_.text) == Some("submit")) =>
@farmdawgnation

farmdawgnation Mar 2, 2013

Owner

Should we really lock this into only being used by an <input type="submit">? I could imagine some uses for other types of inputs having such an onclick handler attached to them (e.g. radios). Unless there's a reason other than our expectation of how this will be useful, why not drop the type="submit" requirement?

@Shadowfiend

Shadowfiend Mar 2, 2013

Owner

This is behavior specifically for form submission. Other inputs should not be used to submit forms. I'm tempted to limit it only to button type="submit"s, too, in fact.

@Shadowfiend

Shadowfiend Mar 2, 2013

Owner

Keep in mind the purpose of this is specifically to append the input name to the outgoing data because AJAX serialization does not include the input name. If you click a radio button, you can attach an event handler client-side that makes it submit the form, and the radio button will be included by virtue of regular serialization.

@farmdawgnation

farmdawgnation Apr 14, 2013

Owner

Sounds good to me.

Owner

farmdawgnation commented Apr 14, 2013

If you have a chance to sow up / respond to my remaining comments we could roll this into the 2.6 development branch. :)

Owner

Shadowfiend commented Apr 14, 2013

Try 'er again.

Owner

farmdawgnation commented Apr 16, 2013

Rebased lift_26_dev onto this branch. Closing 'er out.

@farmdawgnation farmdawgnation deleted the asc_issue_1414 branch Apr 16, 2013

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment