Jesse and I briefly discussed this on the channel last week. I asked about the possibility of having different behavior on an action based upon which button was pressed. For example, having a Preview and Submit form on a comment. I have gotten this to work like this:
<br><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">if (get 'preview') {</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">
div {</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> { class is 'preview comment' }</span><br style="font-family: courier new,monospace;"><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> # More code to render a preview here...</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> </span>
<br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> };</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">};</span><br style="font-family: courier new,monospace;">
<br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">div {</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> { class is 'edit comment' }
</span><br style="font-family: courier new,monospace;"><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> render_action $action;</span><br style="font-family: courier new,monospace;">
<br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> div {</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> { class is 'submit-buttons' }
</span><br style="font-family: courier new,monospace;"><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> form_submit</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> label => _('Preview'),</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> name => 'op',
</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> class => 'first',</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">
onclick => {</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> refresh_self => 1,</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> submit => $action,</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> args => { preview => 1 },
</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> },</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">
;</span><br style="font-family: courier new,monospace;"><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> if (get 'preview') {</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> $action->button(</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> label => _('Submit'),
</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> submit => $action,</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">
parameters => { </span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> 'J:A:F-submit-'.$action->moniker => 1,</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> },</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> );</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> }</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> };</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">};</span><br><br>The way this trick works is as follows: (1) I use a region argument named "preview" to tell the region to expect a preview, (2) the action will fail unless the "submit" parameter is set to a true value, (3) I use the "parameters" field on the submit button to set the submit value of the form. The trick is messy because it exposes some of Jifty's guts to submit the parameter and when you finish and press Submit it goes a little wonky because the region on the next page doesn't get reset quite correctly.
<br><br>First, if someone knows of a good way to do this in Jifty, I'd love to know about it as I haven't thought of a better way. <br><br>Second, I'd like to propose adding to the API a way of submitting additional action parameters on the button. It should be pretty easy to add since Jifty already handles the "parameters" bit here without much of a problem. My proposal is to add a new accessor to Jifty::Web::Form::Clickable: "submit_args" that could be used like this:
<br><br><span style="font-family: courier new,monospace;">$action->button(</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> label => _('Submit'),</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> submit => $action,</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> submit_args => [ $action => { submit => 1 } ],</span><span style="font-family: courier new,monospace;"></span><br>)<span style="font-family: courier new,monospace;">
;<br><br></span>As you can see I've now provided by Submit button without exposing Jifty's "J:A:F" parameter nomenclature. The "submit_args" would be an array ref where the even indexes are the names of the actions or monikers being submitted and the odd indexes are a hash of arguments to add. As a shortcut, it would be possible to drop "submit" in cases where "submit_args" is used:
<br><br><span style="font-family: courier new,monospace;">$action->button(</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> label => _('Submit'),</span><span style="font-family: courier new,monospace;"></span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> submit_args => [ $action => { submit => 1 } ],</span><span style="font-family: courier new,monospace;"></span><br>
)<span style="font-family: courier new,monospace;">;</span><br><br>Similarly, the "onclick" and related handlers of Jifty::Web::Form::Element should be modified to match, so that I could then perform:<br><br><span style="font-family: courier new,monospace;">
$action->button(</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> label => _('Submit'),</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">
onclick => {</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> submit_args => [ $action => { submit => 1 } ],</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> },</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">);</span><br><br>This would then perform the same action as above but allow me to use a JavaScript handler to perform the submit with arguments rather than a regular form post.
<br><br>In addtion to allowing two buttons to have different behaviors on the same action, this would allow for hidden parameters without having to add a separate input to the form. I see a potential here for more advanced link-based action submissions as well.
<br><br>Comments?<br><br>Cheers,<br>Sterling<br>