[jifty-devel] Suggested API change for clickables/onclick handlers

Andrew Sterling Hanenkamp sterling at hanenkamp.com
Mon Oct 8 21:07:55 EDT 2007


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:

if (get 'preview') {
    div {
        { class is 'preview comment' }

        # More code to render a preview here...

    };
};

div {
    { class is 'edit comment' }

    render_action $action;

    div {
        { class is 'submit-buttons' }

        form_submit
            label => _('Preview'),
            name => 'op',
            class => 'first',
            onclick => {
                refresh_self => 1,
                submit => $action,
                args => { preview => 1 },
            },
            ;

        if (get 'preview') {
            $action->button(
                label => _('Submit'),
                submit => $action,
                parameters => {
                    'J:A:F-submit-'.$action->moniker => 1,
                },
            );
        }
    };
};

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.

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.

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:

$action->button(
    label => _('Submit'),
    submit => $action,
    submit_args => [ $action => { submit => 1 } ],
);

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:

$action->button(
    label => _('Submit'),
    submit_args => [ $action => { submit => 1 } ],
);

Similarly, the "onclick" and related handlers of Jifty::Web::Form::Element
should be modified to match, so that I could then perform:

$action->button(
    label => _('Submit'),
    onclick => {
        submit_args => [ $action => { submit => 1 } ],
    },
);

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.

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.

Comments?

Cheers,
Sterling
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.bestpractical.com/pipermail/jifty-devel/attachments/20071008/fcdbfcb0/attachment.htm


More information about the jifty-devel mailing list