Mercurial > pub > Impl
annotate Lib/IMPL/Web/Handler/JSONView.pm @ 370:cbf4febf0930
ObjectMeta, Tests, migrating to the new metadata model.
author | sergey |
---|---|
date | Tue, 10 Dec 2013 03:02:01 +0400 |
parents | 52aae1b85084 |
children |
rev | line source |
---|---|
198 | 1 package IMPL::Web::Handler::JSONView; |
2 use strict; | |
3 use JSON; | |
4 | |
256
32aceba4ee6d
corrected ViewHandlers to handle cookies and headers.
sergey
parents:
241
diff
changeset
|
5 use IMPL::lang qw(is); |
297 | 6 use IMPL::Const qw(:prop); |
198 | 7 use IMPL::declare { |
233 | 8 require => { |
241
f48a1a9f4fa2
+Added ViewResult to allow implementation of the view environment.
sergey
parents:
233
diff
changeset
|
9 HttpResponse => 'IMPL::Web::HttpResponse', |
297 | 10 ViewResult => '-IMPL::Web::ViewResult', |
11 Loader => 'IMPL::Code::Loader' | |
233 | 12 }, |
13 base => [ | |
198 | 14 'IMPL::Object' => undef, |
15 'IMPL::Object::Serializable' => undef, | |
16 'IMPL::Object::Autofill' => '@_' | |
297 | 17 ], |
18 props => [ | |
19 transform => PROP_RW | |
233 | 20 ] |
198 | 21 }; |
22 | |
206 | 23 sub contentType { |
24 'application/json' | |
25 } | |
26 | |
198 | 27 sub Invoke { |
28 my ($this,$action,$next) = @_; | |
29 | |
206 | 30 my $result = $next ? $next->($action) : undef; |
241
f48a1a9f4fa2
+Added ViewResult to allow implementation of the view environment.
sergey
parents:
233
diff
changeset
|
31 |
198 | 32 |
256
32aceba4ee6d
corrected ViewHandlers to handle cookies and headers.
sergey
parents:
241
diff
changeset
|
33 my $model = ( ref $result and is($result,ViewResult) ) |
241
f48a1a9f4fa2
+Added ViewResult to allow implementation of the view environment.
sergey
parents:
233
diff
changeset
|
34 ? $result->model |
f48a1a9f4fa2
+Added ViewResult to allow implementation of the view environment.
sergey
parents:
233
diff
changeset
|
35 : $result; |
f48a1a9f4fa2
+Added ViewResult to allow implementation of the view environment.
sergey
parents:
233
diff
changeset
|
36 |
f48a1a9f4fa2
+Added ViewResult to allow implementation of the view environment.
sergey
parents:
233
diff
changeset
|
37 $model = [$model] unless ref $model; |
f48a1a9f4fa2
+Added ViewResult to allow implementation of the view environment.
sergey
parents:
233
diff
changeset
|
38 |
297 | 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 } | |
44 | |
256
32aceba4ee6d
corrected ViewHandlers to handle cookies and headers.
sergey
parents:
241
diff
changeset
|
45 my %params = ( |
233 | 46 type => $this->contentType, |
47 charset => 'utf-8', | |
241
f48a1a9f4fa2
+Added ViewResult to allow implementation of the view environment.
sergey
parents:
233
diff
changeset
|
48 body => JSON->new->utf8->pretty->encode($model) |
256
32aceba4ee6d
corrected ViewHandlers to handle cookies and headers.
sergey
parents:
241
diff
changeset
|
49 ); |
32aceba4ee6d
corrected ViewHandlers to handle cookies and headers.
sergey
parents:
241
diff
changeset
|
50 |
32aceba4ee6d
corrected ViewHandlers to handle cookies and headers.
sergey
parents:
241
diff
changeset
|
51 if(is($result,ViewResult)) { |
32aceba4ee6d
corrected ViewHandlers to handle cookies and headers.
sergey
parents:
241
diff
changeset
|
52 $params{status} = $result->status if $result->status; |
32aceba4ee6d
corrected ViewHandlers to handle cookies and headers.
sergey
parents:
241
diff
changeset
|
53 $params{headers} = $result->headers if $result->headers; |
32aceba4ee6d
corrected ViewHandlers to handle cookies and headers.
sergey
parents:
241
diff
changeset
|
54 $params{cookies} = $result->cookies if $result->cookies; |
32aceba4ee6d
corrected ViewHandlers to handle cookies and headers.
sergey
parents:
241
diff
changeset
|
55 } |
32aceba4ee6d
corrected ViewHandlers to handle cookies and headers.
sergey
parents:
241
diff
changeset
|
56 |
32aceba4ee6d
corrected ViewHandlers to handle cookies and headers.
sergey
parents:
241
diff
changeset
|
57 return HttpResponse->new( |
32aceba4ee6d
corrected ViewHandlers to handle cookies and headers.
sergey
parents:
241
diff
changeset
|
58 %params |
32aceba4ee6d
corrected ViewHandlers to handle cookies and headers.
sergey
parents:
241
diff
changeset
|
59 ); |
198 | 60 } |
61 | |
62 1; | |
63 | |
64 __END__ | |
65 | |
66 =pod | |
67 | |
68 =head1 | |
69 | |
70 =cut |