Probably not the most pretty one but:
Given you have a resultset obtained with, say,
my $result_set = $c->model('DB::Somedata')->search_rs({...}, {...});
$c->stash->{json} = [
map { id => $_->id, label => $_->name } ($result_set->all)
];
Or a bit more verbose:
foreach my $result ($result_set->all) {
push(@{$c->stash->{json}}, { id => $result->id, label => $result->name });
}
`
Hetényi Csaba wrote:
Ahh!
That was the trick :)))
(expose_stash => 'json')
Now it is correctly translate the perl arrayref datastructure to JSON
array ( [ ... ] )
my @aoh = (
{
value => "1",
label => "betty",
},
{
value => "2",
label => "jane",
},
{
value => "3",
label => "marge",
},
);
$c->stash->{json} = \...@aoh;
$c->forward('View::JSON');
:)
Thanks Ben!
One last question: if I have a DBIx resultset, how to use it to
populate autocomplete with the easiest way?
Best wishes from Hungary!
Ben van Staveren írta:
You can do that with View::JSON :) Just read the docs though, the
reason it didn't work is that you put your data in
$c->stash->{json} = [ ... ]
And View::JSON will attempt to turn your entire stash into JSON data
:) And it does that bit correctly, but the thing you end up with is
{
json: [ ... ]
}
And autocomplete doesn't like that. So, either do:
$c->stash([ ... ])
Or set the expose_stash setting. Whatever is set in there, is the
only thing that View::JSON will attempt to serialise.
Personally I prefer expose_stash => 'json', so that anything under
$c->stash->{json} is serialised, but up to you :)
_______________________________________________
List: [email protected]
Listinfo: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/catalyst
Searchable archive:
http://www.mail-archive.com/[email protected]/
Dev site: http://dev.catalyst.perl.org/
--
Ben van Staveren
phone: +62 81 70777529
email: [email protected]
_______________________________________________
List: [email protected]
Listinfo: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/catalyst
Searchable archive: http://www.mail-archive.com/[email protected]/
Dev site: http://dev.catalyst.perl.org/