[Catalyst] RE: DBIC <-> JSON conversion for AJAX

Will Crawford billcrawford1970 at gmail.com
Tue Sep 13 16:04:47 GMT 2011


On 12 September 2011 13:39,  <Ian.Docherty at nomura.com> wrote:

> my $response = {
>    foo     = $object->foo,
>    bar     = $object->bar,
>    bam     = $object->bam,
> };
> $c->stash->{ajax_response} = $response;
>
> It should also be possible to create a 'flatten' method that does this for you in a generic manner, you could then have.
>
> $c->stash->{ajax_response} = $object->flatten;

I use a method looking like:

    sub quick_resultset {
        return $_[0]->search_rs(undef, { result_class =>
'DBIx::Class::ResultClass::HashRefInflator' });
    }

in our base result set class ("use Moose; extends
'DBIx::Class::ResultSet';"), so it's available for any result set we
get from our model; then you'd just go

    my $object = $c->model('MyModel::Foo')->quick_resultset->find($object_id);

You may want to call it something different :)



More information about the Catalyst mailing list