Catalyst-View-JSON-PerRequest
view release on metacpan or search on metacpan
lib/Catalyst/View/JSON/PerRequest.pm view on Meta::CPAN
return \%init;
});
has json_extra_init_args => (
is=>'ro',
predicate=>'has_json_extra_init_args');
has callback_param => ( is=>'ro', predicate=>'has_callback_param');
sub COMPONENT {
my ($class, $app, $args) = @_;
$args = $class->merge_config_hashes($class->config, $args);
$class->_inject_default_view_model_into($app);
return $class->new($app, $args);
}
sub _inject_default_view_model_into {
my ($class, $app) = @_;
CatalystX::InjectComponent->inject(
into => $app,
component => 'Catalyst::Model::JSON::ViewData',
as => 'Model::JSON::ViewData' );
}
sub build_per_context_instance {
my ($self, $c, @args) = @_;
return bless +{
ctx=>$c,
parent=>$self,
json=>$self->json,
($self->has_handle_encode_error ? (handle_encode_error=>$self->handle_encode_error) :()),
($self->has_callback_param ? (callback_param=>$self->callback_param) :())
}, 'Catalyst::View::JSON::_PerRequest';
}
1;
=head1 NAME
Catalyst::View::JSON::PerRequest - JSON View that owns its data
=head1 SYNOPSIS
MyApp->inject_components(
'View::JSON' => { from_component => 'Catalyst::View::JSON::PerRequest' }
);
# In a controller...
sub root :Chained(/) CaptureArgs(0) {
my ($self, $c) = @_;
$c->view('JSON')->data->set(z=>1);
}
sub midpoint :Chained(root) CaptureArgs(0) {
my ($self, $c) = @_;
$c->view('JSON')->data->set(y=>1);
}
sub endpoint :Chained(midpoint) Args(0) {
my ($self, $c) = @_;
$c->view('JSON')->created({
a => 1,
b => 2,
c => 3,
});
}
=head1 DESCRIPTION
This is a L<Catalyst::View> that produces JSON response from a given model.
It differs from some of the more classic JSON producing views (such as
L<Catalyst::View::JSON> in that is is a per request view (one view for each
request) and it defines a 'data' method to hold information to use to produce
a view.
It also generates some local response helpers. You may or may not find this
approach leads to cleaner code.
=head1 METHODS
This view defines the following methods
=head2 data (?$model)
Used to set the view data model, and/or to called methods on it (for example
to set attributes that will later be used in the JSON response.).
The default is an injected model based on L<Catalyst::Model::JSON::ViewData>
which you should review for basic usage. I recommend setting it to a custom
model that better encapsulates your view data. You may use any model in your
L<Catalyst> application as long as it does the method "TO_JSON".
You may only set the view data model once. If you don't set it and just call
methods on it, the default view model is automatically used.
B<NOTE> In order to help prevent namespace collision, your custom view model is
allowed to defined a method 'set' which is used to set attribute values on your
model. Set should take two arguments, a key and a value.
=head2 res
=head2 response
$view->response($status, @headers, \%data||$object);
$view->response($status, \%data||$object);
$view->response(\%data||$object);
$view->response($status);
$view->response($status, @headers);
Used to setup a response. Calling this method will setup an http status, finalize
headers and set a body response for the JSON. Content type will be set to
'application/json' automatically (you don't need to set this in a header).
=head2 Method '->response' Helpers
We map status codes from L<HTTP::Status> into methods to make sending common
request types more simple and more descriptive. The following are the same:
$c->view->response(200, @args);
( run in 0.783 second using v1.01-cache-2.11-cpan-39bf76dae61 )