Solo has asked for the wisdom of the Perl Monks concerning the following question:
What I want is to include with the declaration of each run mode, the set of next user-selectable run modes. Then from that list, (nearly) automagically add submit buttons to the page.
I'm in the midst of writing a Web+DB app now (with C::A, H::T and CGI::FormBuilder). I've written a tiny subclass of C::A that addresses this Annoyance--at present only with C::FB in tow.
Here's what I came up with:
### in NextRMS.pm package CGI::Application::NextRMS; # I am module-naming challenged use base 'CGI::Application'; sub nextrms { my $self = shift; my %args = (@_); my %run_modes; my %nextrms = exists $self->{__NEXTRMS} ? %{$self->{__NEXTRMS}} : {}; for my $key (keys %args) { $run_modes{$key} = exists $args{$key}{handler} ? $args{$key}{handler} : $key; #little feature for lazy me $nextrms{$key} = exists $args{$key}{nextrms} ? $args{$key}{nextrms} : []; } $self->run_modes(%run_modes); $self->{__NEXTRMS} = \%nextrms; } 1; ### In "webapp.pm"... package WebApp; # use base 'CGI::Application::NextRMS'; push @ISA, 'CGI::Application::NextRMS'; use CGI::Carp qw/ fatalsToBrowser /; use CGI::FormBuilder; sub setup { my $self = shift; $self->start_mode('List'); $self->mode_param('_submit'); # compat w/ C::FB $self->nextrms( Add => { handler => 'add', nextrms => ['Cancel Add','Save'], }, Save => { handler => 'db_create', nextrms => ['List'], }, 'Cancel Add' => { handler => 'list', nextrms => ['Add'], }, List => { handler => 'list', nextrms => ['Add'], }, ); $self->{form} = CGI::FormBuilder->new( keepextras => 1, method => 'POST', name => 'myform', params => $self->query(), fields => [qw/ Test /], validate => {}, ); } sub main_form { my $self = shift; return "Runmode: " . $self->get_current_runmode() . "<br>Dispatched sub: " . (caller(1))[3] . $self->{form}->render( submit => $self->{__NEXTRMS}->{$self->get_current_runmode +()}, ); } sub list { my $self = shift; return $self->main_form(); # or a list_form() :P } sub add { my $self = shift; return $self->main_form(); } sub db_create { my $self = shift; return $self->main_form(); } 1; ### In "webapp.cgi"... #use WebApp; my $webapp = WebApp->new(); $webapp->run();
I didn't find anything lightweight like this in my searches, if anyone has seen similar, please share!
Ideas for better 'next run mode' abstraction and general methodology are appreciated!
TIA
UPDATE: Change first code comment
--
Without precise calculations we could fly right through a star or bounce too close to a supernova and that'd end your trip real quick, wouldn't it?
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: CGI::Application next run mode buttons?
by flyingmoose (Priest) on Apr 02, 2004 at 20:18 UTC | |
by Solo (Deacon) on Apr 02, 2004 at 22:27 UTC | |
Re: CGI::Application next run mode buttons?
by dragonchild (Archbishop) on Apr 02, 2004 at 19:20 UTC | |
by Solo (Deacon) on Apr 02, 2004 at 19:34 UTC | |
Re: CGI::Application next run mode buttons?
by Solo (Deacon) on Apr 05, 2004 at 16:58 UTC | |
by knowmad (Monk) on Apr 09, 2004 at 17:36 UTC |