[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[cgiapp] C::A next run mode buttons?


I find it an Annoyance of C::A that while the current state is abstracted with run_mode, there is no similar abstraction for moving between states. I have to manually code buttons, hyperlinks or hard-code the next state in a hidden field.

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 RunMap.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
--Matt

** also posted on PerlMonks: http://perlmonks.org/index.pl?node_id=342088

---------------------------------------------------------------------
Web Archive:  http://www.mail-archive.com/suppressed/
              http://marc.theaimsgroup.com/?l=cgiapp&r=1&w=2
To unsubscribe, e-mail: suppressed
For additional commands, e-mail: suppressed


Mail converted by mhonarc 2.6.15
This archive provided courtesy of JSW4.NET, Internet Hosting Services for Small Business.