[Catalyst-commits] r14413 -
trunk/examples/CatalystAdvent/root/2012/pen
skaufman at dev.catalyst.perl.org
skaufman at dev.catalyst.perl.org
Wed Dec 5 15:13:55 GMT 2012
Author: skaufman
Date: 2012-12-05 15:13:55 +0000 (Wed, 05 Dec 2012)
New Revision: 14413
Modified:
trunk/examples/CatalystAdvent/root/2012/pen/action_role_JSON.pod
Log:
Updated article, namespacing, conclusion
Modified: trunk/examples/CatalystAdvent/root/2012/pen/action_role_JSON.pod
===================================================================
--- trunk/examples/CatalystAdvent/root/2012/pen/action_role_JSON.pod 2012-12-04 19:27:35 UTC (rev 14412)
+++ trunk/examples/CatalystAdvent/root/2012/pen/action_role_JSON.pod 2012-12-05 15:13:55 UTC (rev 14413)
@@ -1,5 +1,5 @@
-=head1 Action Roles for cleaner less stashy Catalyst Actions
+=head1 Action Roles for cleaner, less stashy Catalyst Actions
=head2 Overview
@@ -9,6 +9,7 @@
Please note that this article leverages behavior that used to only exist in L<Catalyst::Controller::ActionRole>
but has been merged into Catalyst proper in Catalyst v5.90013 .
+
To take advantage of ActionRoles with an older Catalyst see the docs for L<Catalyst::Controller::ActionRole>.
=head2 { hello => 'world' }
@@ -28,8 +29,10 @@
}
It's readable and extensible.
-Write a Catalyst action that does the same.
+Now re-written as a Catalyst Ajax endpoint:
+
+
sub hello {
my ( $self, $ctx ) = @_;
my $name = $ctx->req->params->{name};
@@ -44,19 +47,11 @@
);
}
-This is now pretty awful. A method that is supposed to take a string and transform it into a hashref is now:
+Pretty gross. Most of the method is no longer concerned with the actual data transformation and is instead doing a bunch of
+crap that you're probably repeating elsewhere.
-=over 2
+When that goes from being ugly to being a pain is when, for example, you decide you want migrate to using L<Catalyst::Controller::REST>.
-=item reading from the global "params"
-
-=item doing the transformation
-
-=item JSON encoding the response
-
-=back
-When that goes from being ugly to being a pain is when you decide you want to use L<Catalyst::Controller::REST>.
-
The method becomes:
sub hello {
@@ -69,7 +64,7 @@
};
}
-And you now have to modify every action that used to use the first method.
+which is cleaner, but now you have to modify every action that used to use the first method.
Let's clean this up a bit.
@@ -78,32 +73,24 @@
__PACKAGE__->config(
action => {
hello => {
- Does => "JSONEncode"
+ Does => "SerializeReturnValue"
}
}
);
-....meanwhile, in lib/WebApp/ActionRole/JSONEncode.pm
+....meanwhile, in lib/WebApp/ActionRole/SerializeReturnValue.pm
- package WebApp::ActionRole::JSONEncode;
+ package WebApp::ActionRole::SerializeReturnValue;
use strictures 1;
use Moose::Role;
- use JSON;
- has json => (
- is => 'ro',
- default => sub { JSON->new }
- );
around execute => sub {
# $self is the $c->action being 'executed'
my ( $orig, $self ) = ( shift, shift );
# execute is called on an action in a controller context
- my ( $controller, $ctx ) = @_;
+ my ( $controller, $ctx ) = @_;
# wrap the original method
- my $ret = $self->$orig(@_);
- $ctx->res->headers->{ContentType} = "text/javascript";
- $ctx->res->body( $self->json->encode( $ret ) );
- return $ret;
+ return $ctx->stash->{rest} = $self->$orig(@_);
};
no Moose::Role;
@@ -121,9 +108,17 @@
)
}
-So while the 'name' param is still being grabbed from a global (we'll leave fixing that for another time and place), the method is returning the intended hash without worrying about the encoding.
+So while the 'name' param is still being grabbed from a global (we'll leave fixing that for another time and place),
+the method is returning the intended hash without worrying about the encoding.
+Now you're sick of L<Catalyst::Controller::Rest> and want to handle serialization yourself?
+No problem, change the ActionRole to JSON encode the return and drop it into the response body,
+or create a JSON (or xml or yml...) view and stash it in another key, or subclass L<Catalyst::Response> and give
+it a 'data' attribute, etc etc TIMDOWDI.
+Point is, you won't have to edit your 'hello' method or your 30 others that return serializable data because you've properly
+encapsulated your methods.
+
=head1 AUTHOR
Samuel Kaufman <skaufman at cpan.org>
More information about the Catalyst-commits
mailing list