Created
August 19, 2008 17:01
-
-
Save beppu/6204 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package WhatIf; | |
use base 'Squatting'; | |
# Data in %state persists between requests. | |
our %state; | |
sub service { | |
my ($app, $c, @args) = @_; | |
my $cr = $c->cr; | |
my $sid = $cr->{session_id}; | |
if (defined $sid) { | |
$c->state = $state{$sid} ||= {}; | |
} | |
$app->next::method($c, @args); | |
} | |
package WhatIf::Controllers; | |
use strict; | |
use Squatting ':controllers'; | |
our @C = ( | |
C( | |
Home => [ '/' ], | |
get => sub { | |
my ($self) = @_; | |
$self->redirect(R('WhatIf')); | |
} | |
), | |
C( | |
WhatIf => [ '/what-if' ], | |
get => sub { | |
my ($self) = @_; | |
my $v = $self->v; | |
my $state = $self->state; | |
$v->{errors} = $state->{errors}; | |
$v->{messages} = $state->{messages}; | |
$v->{response} = $state->{response}; | |
my $content = $self->render('form'); | |
delete($state->{errors}); | |
delete($state->{messages}); | |
$content; | |
}, | |
post => sub { | |
my ($self) = @_; | |
my $response = $self->input->{response}; | |
if ($response =~ /^\w+$/) { | |
$self->state->{messages} = "'$response' matches the regex."; | |
} else { | |
$self->state->{errors} = "'$response' does not match the regex."; | |
} | |
$self->state->{response} = $response; | |
$self->redirect(R('WhatIf')); | |
}, | |
), | |
); | |
package WhatIf::Views; | |
use strict; | |
use Squatting ':views'; | |
use HTML::AsSubs; | |
sub span { HTML::AsSubs::_elem('span', @_) } | |
sub x { map { HTML::Element->new('~literal', text => $_) } @_ } | |
our @V = ( | |
V( | |
'html', | |
layout => sub { | |
my ($self, $v, $content) = @_; | |
html( | |
head( | |
title('Using Sessions to Preserve State Between Requests'), | |
style(x($self->_css)), | |
), | |
body( | |
div({ id => 'notifications' }, | |
( $v->{messages} && div({ id => 'messages' }, $v->{messages}) ), | |
( $v->{errors} && div({ id => 'errors' }, $v->{errors}) ), | |
), | |
div({ id => 'content' }, | |
x( $content ), | |
), | |
), | |
)->as_HTML; | |
}, | |
_css => sub {qq| | |
body { | |
background: #111; | |
color: #fff; | |
font-family: "Trebuchet MS", sans-serif; | |
font-size: 11pt; | |
} | |
input { | |
font-family: "Trebuchet MS", sans-serif; | |
font-size: 15pt; | |
border: 1px solid #888; | |
} | |
h1 { | |
color: #2c9; | |
} | |
#messages { | |
padding: 8px; | |
width: 420px; | |
background: #8f8; | |
color: #000; | |
border: 1px solid #2f2; | |
} | |
#errors { | |
padding: 8px; | |
width: 420px; | |
background: #f88; | |
color: #000; | |
border: 1px solid #f22; | |
} | |
|}, | |
form => sub { | |
my ($self, $v) = @_; | |
form( | |
{ method => 'post', action => R('WhatIf') }, | |
h1('Enter a string that matches /^\w+$/ .'), | |
div( | |
input( | |
{ | |
type => 'text', | |
name => 'response', | |
value => $v->{response} || '', | |
} | |
), | |
input( | |
{ type => 'submit', name => 'submit', value => 'OK' } | |
), | |
), | |
)->as_HTML; | |
}, | |
), | |
); | |
1; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment