Mercurial > pub > Impl
annotate Lib/IMPL/Web/Handler/JSONView.pm @ 263:0f59b2de72af
*fixed IMPL::DOM::Schema circular module references
*modified IMPL::Object::Singleton, added auto-activation
*code cleanups, docs
author | sergey |
---|---|
date | Wed, 09 Jan 2013 05:17:44 +0400 |
parents | 32aceba4ee6d |
children | 52aae1b85084 |
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); |
198 | 6 use IMPL::declare { |
233 | 7 require => { |
241
f48a1a9f4fa2
+Added ViewResult to allow implementation of the view environment.
sergey
parents:
233
diff
changeset
|
8 HttpResponse => 'IMPL::Web::HttpResponse', |
f48a1a9f4fa2
+Added ViewResult to allow implementation of the view environment.
sergey
parents:
233
diff
changeset
|
9 ViewResult => '-IMPL::Web::ViewResult' |
233 | 10 }, |
11 base => [ | |
198 | 12 'IMPL::Object' => undef, |
13 'IMPL::Object::Serializable' => undef, | |
14 'IMPL::Object::Autofill' => '@_' | |
233 | 15 ] |
198 | 16 }; |
17 | |
206 | 18 sub contentType { |
19 'application/json' | |
20 } | |
21 | |
198 | 22 sub Invoke { |
23 my ($this,$action,$next) = @_; | |
24 | |
206 | 25 my $result = $next ? $next->($action) : undef; |
241
f48a1a9f4fa2
+Added ViewResult to allow implementation of the view environment.
sergey
parents:
233
diff
changeset
|
26 |
198 | 27 |
256
32aceba4ee6d
corrected ViewHandlers to handle cookies and headers.
sergey
parents:
241
diff
changeset
|
28 my $model = ( ref $result and is($result,ViewResult) ) |
241
f48a1a9f4fa2
+Added ViewResult to allow implementation of the view environment.
sergey
parents:
233
diff
changeset
|
29 ? $result->model |
f48a1a9f4fa2
+Added ViewResult to allow implementation of the view environment.
sergey
parents:
233
diff
changeset
|
30 : $result; |
f48a1a9f4fa2
+Added ViewResult to allow implementation of the view environment.
sergey
parents:
233
diff
changeset
|
31 |
f48a1a9f4fa2
+Added ViewResult to allow implementation of the view environment.
sergey
parents:
233
diff
changeset
|
32 $model = [$model] unless ref $model; |
f48a1a9f4fa2
+Added ViewResult to allow implementation of the view environment.
sergey
parents:
233
diff
changeset
|
33 |
256
32aceba4ee6d
corrected ViewHandlers to handle cookies and headers.
sergey
parents:
241
diff
changeset
|
34 my %params = ( |
233 | 35 type => $this->contentType, |
36 charset => 'utf-8', | |
241
f48a1a9f4fa2
+Added ViewResult to allow implementation of the view environment.
sergey
parents:
233
diff
changeset
|
37 body => JSON->new->utf8->pretty->encode($model) |
256
32aceba4ee6d
corrected ViewHandlers to handle cookies and headers.
sergey
parents:
241
diff
changeset
|
38 ); |
32aceba4ee6d
corrected ViewHandlers to handle cookies and headers.
sergey
parents:
241
diff
changeset
|
39 |
32aceba4ee6d
corrected ViewHandlers to handle cookies and headers.
sergey
parents:
241
diff
changeset
|
40 if(is($result,ViewResult)) { |
32aceba4ee6d
corrected ViewHandlers to handle cookies and headers.
sergey
parents:
241
diff
changeset
|
41 $params{status} = $result->status if $result->status; |
32aceba4ee6d
corrected ViewHandlers to handle cookies and headers.
sergey
parents:
241
diff
changeset
|
42 $params{headers} = $result->headers if $result->headers; |
32aceba4ee6d
corrected ViewHandlers to handle cookies and headers.
sergey
parents:
241
diff
changeset
|
43 $params{cookies} = $result->cookies if $result->cookies; |
32aceba4ee6d
corrected ViewHandlers to handle cookies and headers.
sergey
parents:
241
diff
changeset
|
44 } |
32aceba4ee6d
corrected ViewHandlers to handle cookies and headers.
sergey
parents:
241
diff
changeset
|
45 |
32aceba4ee6d
corrected ViewHandlers to handle cookies and headers.
sergey
parents:
241
diff
changeset
|
46 return HttpResponse->new( |
32aceba4ee6d
corrected ViewHandlers to handle cookies and headers.
sergey
parents:
241
diff
changeset
|
47 %params |
32aceba4ee6d
corrected ViewHandlers to handle cookies and headers.
sergey
parents:
241
diff
changeset
|
48 ); |
198 | 49 } |
50 | |
51 1; | |
52 | |
53 __END__ | |
54 | |
55 =pod | |
56 | |
57 =head1 | |
58 | |
59 =cut |