},
},
'Plugin::Session' => {
- flash_to_stash => 1
- }
+ flash_to_stash => 1,
+ },
+ default_view => 'HTML',
+ 'View::JSON' => {
+ expose_stash => 'json',
+ },
);
# Start the application
#
__PACKAGE__->config(namespace => '');
-use JSON 'encode_json';
-
use Chatty::Form::Login;
use Chatty::Form::Register;
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
--- /dev/null
+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;