annotate Lib/IMPL/Web/Handler/JSONView.pm @ 295:9b25e1f50144

sync
author cin
date Fri, 01 Mar 2013 03:15:30 +0400
parents 32aceba4ee6d
children 52aae1b85084
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
198
2ffe6f661605 Implemented IMPL::Web::Handler::RestController
cin
parents:
diff changeset
1 package IMPL::Web::Handler::JSONView;
2ffe6f661605 Implemented IMPL::Web::Handler::RestController
cin
parents:
diff changeset
2 use strict;
2ffe6f661605 Implemented IMPL::Web::Handler::RestController
cin
parents:
diff changeset
3 use JSON;
2ffe6f661605 Implemented IMPL::Web::Handler::RestController
cin
parents:
diff changeset
4
256
32aceba4ee6d corrected ViewHandlers to handle cookies and headers.
sergey
parents: 241
diff changeset
5 use IMPL::lang qw(is);
198
2ffe6f661605 Implemented IMPL::Web::Handler::RestController
cin
parents:
diff changeset
6 use IMPL::declare {
233
3cebcf6fdb9b refactoring, cleaning code
sergey
parents: 206
diff changeset
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
3cebcf6fdb9b refactoring, cleaning code
sergey
parents: 206
diff changeset
10 },
3cebcf6fdb9b refactoring, cleaning code
sergey
parents: 206
diff changeset
11 base => [
198
2ffe6f661605 Implemented IMPL::Web::Handler::RestController
cin
parents:
diff changeset
12 'IMPL::Object' => undef,
2ffe6f661605 Implemented IMPL::Web::Handler::RestController
cin
parents:
diff changeset
13 'IMPL::Object::Serializable' => undef,
2ffe6f661605 Implemented IMPL::Web::Handler::RestController
cin
parents:
diff changeset
14 'IMPL::Object::Autofill' => '@_'
233
3cebcf6fdb9b refactoring, cleaning code
sergey
parents: 206
diff changeset
15 ]
198
2ffe6f661605 Implemented IMPL::Web::Handler::RestController
cin
parents:
diff changeset
16 };
2ffe6f661605 Implemented IMPL::Web::Handler::RestController
cin
parents:
diff changeset
17
206
c8fe3f84feba +IMPL::Web::Handlers::ViewSelector
sergey
parents: 199
diff changeset
18 sub contentType {
c8fe3f84feba +IMPL::Web::Handlers::ViewSelector
sergey
parents: 199
diff changeset
19 'application/json'
c8fe3f84feba +IMPL::Web::Handlers::ViewSelector
sergey
parents: 199
diff changeset
20 }
c8fe3f84feba +IMPL::Web::Handlers::ViewSelector
sergey
parents: 199
diff changeset
21
198
2ffe6f661605 Implemented IMPL::Web::Handler::RestController
cin
parents:
diff changeset
22 sub Invoke {
2ffe6f661605 Implemented IMPL::Web::Handler::RestController
cin
parents:
diff changeset
23 my ($this,$action,$next) = @_;
2ffe6f661605 Implemented IMPL::Web::Handler::RestController
cin
parents:
diff changeset
24
206
c8fe3f84feba +IMPL::Web::Handlers::ViewSelector
sergey
parents: 199
diff changeset
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
2ffe6f661605 Implemented IMPL::Web::Handler::RestController
cin
parents:
diff changeset
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
3cebcf6fdb9b refactoring, cleaning code
sergey
parents: 206
diff changeset
35 type => $this->contentType,
3cebcf6fdb9b refactoring, cleaning code
sergey
parents: 206
diff changeset
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
2ffe6f661605 Implemented IMPL::Web::Handler::RestController
cin
parents:
diff changeset
49 }
2ffe6f661605 Implemented IMPL::Web::Handler::RestController
cin
parents:
diff changeset
50
2ffe6f661605 Implemented IMPL::Web::Handler::RestController
cin
parents:
diff changeset
51 1;
2ffe6f661605 Implemented IMPL::Web::Handler::RestController
cin
parents:
diff changeset
52
2ffe6f661605 Implemented IMPL::Web::Handler::RestController
cin
parents:
diff changeset
53 __END__
2ffe6f661605 Implemented IMPL::Web::Handler::RestController
cin
parents:
diff changeset
54
2ffe6f661605 Implemented IMPL::Web::Handler::RestController
cin
parents:
diff changeset
55 =pod
2ffe6f661605 Implemented IMPL::Web::Handler::RestController
cin
parents:
diff changeset
56
2ffe6f661605 Implemented IMPL::Web::Handler::RestController
cin
parents:
diff changeset
57 =head1
2ffe6f661605 Implemented IMPL::Web::Handler::RestController
cin
parents:
diff changeset
58
2ffe6f661605 Implemented IMPL::Web::Handler::RestController
cin
parents:
diff changeset
59 =cut