[Catalyst] TT via AJAX

Matthew Phillips mattp at cpan.org
Wed Apr 25 13:56:09 GMT 2012


Alternatively, you can use
https://metacpan.org/module/DBIx::Class::Helper::Row::ToJSON to allow for
automatic serialization.

Cheers,
Matt

On Wed, Apr 25, 2012 at 9:53 AM, will trillich
<will.trillich at serensoft.com>wrote:

> Roland --
>
> Assuming your JSON view is trying to encode the 'result' item from your
> stash, first let's see why encoding a blessed object is fraught with peri=
l.
>
> Try running your server in debug mode, and when you get to the ->stash()
> line, try "x $rs". You really want all that JSON-encoded?
>
> Blessed objects (such as a recordset or a request or a response) typically
> contain many, many layers that are often going to be overkill when encodi=
ng
> stuff as JSON, when really you just want a few top-level items.
>
> So you should iterate through your objects and/or collections and create a
> hashref that contains plain primitives (or maybe a few more hashrefs or
> arrayrefs when needed) instead of asking JSON to translate blessed objects
> that are dozens of layers deep.
>
> Instead of doing ...->stash( result =3D> [ $rs ] ) build your own data
> structure, by doing something like
>
> my $json =3D {
>   data =3D> [],
>   count=3D> $rs->count,
>   other =3D> $self->blah,
> };
> while ( my $rec =3D $rs->next ) {
>   my %fields =3D $self->get_important_values( $rec );
>   push @{ $json->{data} }, { %fields );
> }
> $c->stash( result =3D> $json );
>
>
>
> On Wed, Apr 25, 2012 at 8:26 AM, Roland Philibert <rphilibert at aptina.com>=
wrote:
>
>> Hello all,****
>>
>> I=92d like to render some HTML content being formatted from a  TT view v=
ia
>> AJAX.
>> Can anybody recommend a way to do that please?****
>>
>> ** **
>>
>> The method I am using now for AJAX  is REST controller and JSON View.****
>>
>> ** **
>>
>> ...****
>>
>> sub ajaxaction :Local :ActionClass('REST') {}****
>>
>> ** **
>>
>> sub ajaxaction _GET {****
>>
>> ** **
>>
>>                 my ($self, $c) =3D @_;****
>>
>>                 my $rs =3D  $c->model(DN)->resultset(Table)->find(...);*=
***
>>
>>                 my $body =3D
>> $c->view(MYTT)->render($c,'dir/temp.mailtt',$c->stash( result  =3D> [ $r=
s ]
>> )); =DF this is the weird bit I guess.****
>>
>>                 my @content =3D();****
>>
>>                 push @content, $body;****
>>
>>                 $self->status_ok(****
>>
>>                         $c,****
>>
>>                         entity =3D> \@content,****
>>
>>                 );****
>>
>> }****
>>
>> sub end :Private {****
>>
>>         my ($self, $c) =3D @_;****
>>
>>         $c->forward("View::JSON");****
>>
>> }****
>>
>> ** **
>>
>> ..but doing this I get the error:****
>>
>> ** **
>>
>> Caught exception in MyApp::View::JSON->process "encountered object
>> 'MyApp::Model::DB::Table=3DHASH(0xdb2c330)', but neither allow_blessed n=
or
>> convert_blessed settings are enabled at (eval 1606) line 151."****
>>
>> ** **
>>
>> Thanks for your help.****
>>
>> Roland****
>>
>> ** **
>>
>> ** **
>>
>> ** **
>>
>> ** **
>>
>> ** **
>>
>> ** **
>>
>> ** **
>>
>> ** **
>>
>> ** **
>>
>> ** **
>>
>> ** **
>>
>> ** **
>>
>> ** **
>>
>> ** **
>>
>> ** **
>>
>> ** **
>>
>> ** **
>>
>> ** **
>>
>> ** **
>>
>> ** **
>>
>> ** **
>>
>> ** **
>>
>> ** **
>>
>> ** **
>>
>> ** **
>>
>> Aptina (UK) Limited, Century Court, Millennium Way, Bracknell, Berkshire=
, RG12 2XT. Registered in England No. 06570543.
>>
>> This e-mail and any attachments contain confidential information and are=
 solely for the review and use of the intended recipient. If you have recei=
ved this e-mail in error, please notify the sender and destroy this e-mail =
and any copies.
>>
>>
>>
>> _______________________________________________
>> List: Catalyst at lists.scsys.co.uk
>> Listinfo: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/catalyst
>> Searchable archive:
>> http://www.mail-archive.com/catalyst@lists.scsys.co.uk/
>> Dev site: http://dev.catalyst.perl.org/
>>
>>
>
>
> --
> "We act as though comfort and luxury were the chief requirements of life,
> when all that we need to make us happy is something to be enthusiastic
> about." -- Albert Einstein
>
>
> _______________________________________________
> List: Catalyst at lists.scsys.co.uk
> Listinfo: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/catalyst
> Searchable archive:
> http://www.mail-archive.com/catalyst@lists.scsys.co.uk/
> Dev site: http://dev.catalyst.perl.org/
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.scsys.co.uk/pipermail/catalyst/attachments/20120425/7a6be=
185/attachment.htm


More information about the Catalyst mailing list