1 |
dpavlin |
946 |
package Frey::View::Exhibit::JSON; |
2 |
|
|
use Moose; |
3 |
|
|
|
4 |
|
|
extends 'Frey'; |
5 |
|
|
with 'Frey::Web'; |
6 |
|
|
#with 'Frey::Storage'; |
7 |
|
|
|
8 |
|
|
has sponge => ( |
9 |
|
|
is => 'rw', |
10 |
|
|
isa => 'Sponge', |
11 |
|
|
required => 1, |
12 |
|
|
); |
13 |
|
|
|
14 |
|
|
use JSON; |
15 |
|
|
|
16 |
|
|
sub as_markup { |
17 |
|
|
my ($self) = @_; |
18 |
|
|
|
19 |
|
|
my $items; |
20 |
|
|
my $names = $self->sponge->{NAME}; |
21 |
|
|
|
22 |
|
|
my $id = 0; |
23 |
|
|
my $have_id = grep { m{^id$} } @$names; |
24 |
|
|
my $have_label = grep { m{^label$} } @$names; |
25 |
|
|
|
26 |
|
|
foreach my $row ( @{ $self->sponge->{rows} } ) { |
27 |
|
|
my $item; |
28 |
|
|
map { $item->{ $names->[$_] } = $row->[$_] } ( 0 .. $#{ $row } ); |
29 |
|
|
$item->{id} = $id unless $have_id; |
30 |
|
|
$item->{label} = $id unless $have_label; |
31 |
|
|
$id++; |
32 |
|
|
push @{ $items->{items} }, $item; |
33 |
|
|
} |
34 |
|
|
|
35 |
|
|
$self->content_type('application/javascript'); |
36 |
|
|
# JSON->new->allow_nonref(1)->allow_blessed(1)->encode($items); |
37 |
|
|
JSON->new->encode($items); |
38 |
|
|
} |
39 |
|
|
|
40 |
|
|
1; |