From 30558f981fe131d1ae3cc88ff5fc7f9e910932e9 Mon Sep 17 00:00:00 2001 From: Charles McGarvey Date: Tue, 3 Jan 2012 19:27:13 -0700 Subject: [PATCH] use a json view instead of writing json directly --- lib/Chatty.pm | 8 ++++++-- lib/Chatty/Controller/Root.pm | 13 ++++-------- lib/Chatty/View/JSON.pm | 37 +++++++++++++++++++++++++++++++++++ t/view_JSON.t | 8 ++++++++ 4 files changed, 55 insertions(+), 11 deletions(-) create mode 100644 lib/Chatty/View/JSON.pm create mode 100644 t/view_JSON.t diff --git a/lib/Chatty.pm b/lib/Chatty.pm index 5b91943..80772e7 100644 --- a/lib/Chatty.pm +++ b/lib/Chatty.pm @@ -52,8 +52,12 @@ __PACKAGE__->config( }, }, 'Plugin::Session' => { - flash_to_stash => 1 - } + flash_to_stash => 1, + }, + default_view => 'HTML', + 'View::JSON' => { + expose_stash => 'json', + }, ); # Start the application diff --git a/lib/Chatty/Controller/Root.pm b/lib/Chatty/Controller/Root.pm index 3b7fa3d..fec3f09 100644 --- a/lib/Chatty/Controller/Root.pm +++ b/lib/Chatty/Controller/Root.pm @@ -10,8 +10,6 @@ BEGIN { extends 'Catalyst::Controller' } # __PACKAGE__->config(namespace => ''); -use JSON 'encode_json'; - use Chatty::Form::Login; use Chatty::Form::Register; @@ -137,22 +135,19 @@ sub register_validate :Local :Args(0) { my $id = $c->req->param('fieldId'); my $username = $c->req->param('fieldValue'); - my $json_arr = []; - if ($username) { my $account = $c->model('DB::Account')->find({username => $username}); if (!$account) { - $json_arr = ["$id", 1, "This username is available. Nice!"]; + $c->stash->{json} = ["$id", 1, "This username is available. Nice!"]; } else { - $json_arr = ["$id", 0, "This username is taken."]; + $c->stash->{json} = ["$id", 0, "This username is taken."]; } } else { - $json_arr = ["$id", 0, "Invalid arguments to check script."]; + $c->stash->{json} = ["$id", 0, "Invalid arguments to check script."]; } - $c->res->content_type("application/json"); - $c->res->body(encode_json($json_arr)); + $c->forward('View::JSON'); } =head2 access_denied diff --git a/lib/Chatty/View/JSON.pm b/lib/Chatty/View/JSON.pm new file mode 100644 index 0000000..30510ca --- /dev/null +++ b/lib/Chatty/View/JSON.pm @@ -0,0 +1,37 @@ +package Chatty::View::JSON; + +use JSON::XS (); + +use Moose; +use MooseX::NonMoose; +use namespace::autoclean; + +extends 'Catalyst::View::JSON'; + +sub encode_json { + my ($self, $c, $data) = @_; + JSON::XS->new->ascii->pretty->allow_nonref->encode($data); +} + +=head1 NAME + +Chatty::View::JSON - Catalyst View + +=head1 DESCRIPTION + +Catalyst View. + +=head1 AUTHOR + +Charles McGarvey + +=head1 LICENSE + +This library is free software. You can redistribute it and/or modify +it under the same terms as Perl itself. + +=cut + +__PACKAGE__->meta->make_immutable; + +1; diff --git a/t/view_JSON.t b/t/view_JSON.t new file mode 100644 index 0000000..52b65e0 --- /dev/null +++ b/t/view_JSON.t @@ -0,0 +1,8 @@ +use strict; +use warnings; +use Test::More; + + +BEGIN { use_ok 'Chatty::View::JSON' } + +done_testing(); -- 2.43.0