comparison Lib/IMPL/Web/Handler/JSONView.pm @ 297:52aae1b85084

Added transformations support to JSONView
author cin
date Mon, 11 Mar 2013 01:22:24 +0400
parents 32aceba4ee6d
children
comparison
equal deleted inserted replaced
296:86ff93b34f2a 297:52aae1b85084
1 package IMPL::Web::Handler::JSONView; 1 package IMPL::Web::Handler::JSONView;
2 use strict; 2 use strict;
3 use JSON; 3 use JSON;
4 4
5 use IMPL::lang qw(is); 5 use IMPL::lang qw(is);
6 use IMPL::Const qw(:prop);
6 use IMPL::declare { 7 use IMPL::declare {
7 require => { 8 require => {
8 HttpResponse => 'IMPL::Web::HttpResponse', 9 HttpResponse => 'IMPL::Web::HttpResponse',
9 ViewResult => '-IMPL::Web::ViewResult' 10 ViewResult => '-IMPL::Web::ViewResult',
11 Loader => 'IMPL::Code::Loader'
10 }, 12 },
11 base => [ 13 base => [
12 'IMPL::Object' => undef, 14 'IMPL::Object' => undef,
13 'IMPL::Object::Serializable' => undef, 15 'IMPL::Object::Serializable' => undef,
14 'IMPL::Object::Autofill' => '@_' 16 'IMPL::Object::Autofill' => '@_'
17 ],
18 props => [
19 transform => PROP_RW
15 ] 20 ]
16 }; 21 };
17 22
18 sub contentType { 23 sub contentType {
19 'application/json' 24 'application/json'
28 my $model = ( ref $result and is($result,ViewResult) ) 33 my $model = ( ref $result and is($result,ViewResult) )
29 ? $result->model 34 ? $result->model
30 : $result; 35 : $result;
31 36
32 $model = [$model] unless ref $model; 37 $model = [$model] unless ref $model;
38
39 if (my $factory = $this->transform) {
40 Loader->safe->Require($factory) unless ref $factory;
41 my $t = $this->transform->new();
42 $model = $t->Transform($model);
43 }
33 44
34 my %params = ( 45 my %params = (
35 type => $this->contentType, 46 type => $this->contentType,
36 charset => 'utf-8', 47 charset => 'utf-8',
37 body => JSON->new->utf8->pretty->encode($model) 48 body => JSON->new->utf8->pretty->encode($model)