[Catalyst] TT via AJAX

will trillich will.trillich at serensoft.com
Thu Apr 26 20:26:40 GMT 2012


Roland -- you probably don't want a resultset converted to JSON notation,
you're much more likely to want the rows and fields of data.

When you're generating an HTML representation you can use the stash since
it's server-side. The TemplateToolkit code can then do stuff like
[% FOREACH r =3D rs %]...

But when you're working with AJAX, your client-side is where the processing
happens, so you won't be able to send a resultset via JSON. It won't have
your data. You must poke your data into the stash so that it gets converted
to JSON properly, and then sent to the client/browser for handling.

sub get_important_values { # depends on your data, e.g.
  my $self =3D shift;
  my $record =3D shift;

  # yadda yadda blah blah
  my $wins =3D $record->win_ct;
  my $losses =3D $record->loss_ct;
  my $games =3D $wins + $losses;

  return {
    fname =3D> $record->first_name,
    # mname <- ignore fields we don't need
    lname =3D> $record->last_name,
    thingies =3D> $record->thingies->count,
    wins =3D> $wins,
    losses =3D> $losses,
    # calculate other useful fields as needed:
    avg =3D> $games ? ( $wins / $games ) : undef,
  };
}

On Thu, Apr 26, 2012 at 2:24 PM, Roland Philibert <rphilibert at aptina.com>wr=
ote:

> Hi Will,****
>
> In your previous suggestion you had:****
>
> ** **
>
>                 while ( my $rec =3D $rs->next ) {****
>
>                                 my %fields =3D $self->get_important_value=
s(
> $rec );****
>
>                                 push @{ $json->{data} }, { %fields );****
>
>                 }****
>
> ** **
>
> =85what is =93get_important_values=94? How to make sure that all the resu=
ltset
> is transferred to $json.****
>
> ** **
>
> Thanks for you help.****
>
> R****
>
> ** **
>
> ** **
>
> ** **
>
> *From:* will trillich [mailto:will.trillich at serensoft.com]
> *Sent:* 26 April 2012 14:02
>
> *To:* The elegant MVC web framework
> *Subject:* Re: [Catalyst] TT via AJAX****
>
> ** **
>
> Roland --****
>
> ** **
>
> The main thing is, a resultset isn't your data, it's a way to *get to*
> your data. At some point your process needs to iterate thru the rows that
> your recordset would return, and that's the part you want in your data.***
> *
>
> ** **
>
> It's not that you're rebuilding the data structure, you just want to get
> the data you need, omitting the other housekeeping items that would clutt=
er
> up your results (and take time to encode).****
>
> ** **
>
> E.g.****
>
> ** **
>
>   DB<3> |x $rs****
>
> 0  DBIx::Class::ResultSet=3DHASH(0xc1e8460)****
>
>    '_result_class' =3D> 'WT::Model::Model::Master'****
>
>    'attrs' =3D> HASH(0xc239ed8)****
>
>       'alias' =3D> 'me'****
>
>       'where' =3D> HASH(0xc1e55e0)****
>
>          'ix' =3D> '-1'****
>
>    'cond' =3D> HASH(0xc1e55e0)****
>
>       -> REUSED_ADDRESS****
>
>    'pager' =3D> undef****
>
>    'result_source' =3D> DBIx::Class::ResultSource::Table=3DHASH(0xbbaa400=
)****
>
>       '_columns' =3D> HASH(0xbbaa7c0)****
>
>          'cat' =3D> HASH(0xbb74b30)****
>
>             'data_type' =3D> 'VARCHAR'****
>
>             'default_value' =3D> undef****
>
>             'is_nullable' =3D> 1****
>
>             'size' =3D> 8****
>
>          'class' =3D> HASH(0xbb74cb0)****
>
>             'data_type' =3D> 'VARCHAR'****
>
>             'default_value' =3D> undef****
>
>             'is_nullable' =3D> 1****
>
>             'size' =3D> 4****
>
> *...snip...300 or more lines later...snip...*****
>
>                'sqlt_deploy_callback' =3D> 'default_sqlt_deploy_hook'****
>
>          'storage' =3D> DBIx::Class::Storage::DBI=3DHASH(0xbba4470)****
>
>             '_connect_info' =3D> ARRAY(0xb9ae738)****
>
>                0  HASH(0xac9f618)****
>
>                   'dsn' =3D> 'dbi:mysql:dbname=3Dwt'****
>
>                   'password' =3D> 'sekrit-goeth-thither'****
>
>                   'user' =3D> 'blahyaddablah'****
>
>             '_dbh_details' =3D> HASH(0xbba78b0)****
>
>                  empty hash****
>
>             '_dbh_gen' =3D> 0****
>
>             '_dbi_connect_info' =3D> ARRAY(0xab32940)****
>
>                0  'dbi:mysql:dbname=3Dwt'****
>
>                1  'blahyaddablah'****
>
>                2  'sekrit-goeth-thither'****
>
>                3  HASH(0xab32860)****
>
>                   'AutoCommit' =3D> 1****
>
>                   'PrintError' =3D> 0****
>
>                   'RaiseError' =3D> 1****
>
>                   'ShowErrorStatement' =3D> 1****
>
>             '_dbic_connect_attributes' =3D> HASH(0xab32860)****
>
>                -> REUSED_ADDRESS****
>
>             '_in_dbh_do' =3D> 0****
>
>             '_sql_maker' =3D> undef****
>
>             '_sql_maker_opts' =3D> HASH(0xbba78d0)****
>
>                  empty hash****
>
>             'savepoints' =3D> ARRAY(0xbba4480)****
>
>                  empty array****
>
>             'schema' =3D> WT::Schema=3DHASH(0xb93e380)****
>
>                -> REUSED_ADDRESS****
>
>             'transaction_depth' =3D> 0****
>
>       'source_name' =3D> 'Master'****
>
>       'sqlt_deploy_callback' =3D> 'default_sqlt_deploy_hook'****
>
> ** **
>
> Encoding all that recordset background gunk as JSON is not going to be
> useful. You just need your data, and $rs->next() is a clean way to get at
> that.****
>
> ** **
>
> ** **
>
> ** **
>
> On Thu, Apr 26, 2012 at 7:21 AM, Roland Philibert <rphilibert at aptina.com>
> wrote:****
>
> Hi Will,****
>
> Thanks for this, am not sure I understand though..****
>
> I don=92t really want to re-build the datastructure as the resultset $rs
> contains all relationships used by the template.****
>
> What I want to achieve is send back $body (from my rendered TT) via JSON
> back to my $.ajax.****
>
> Any example on how to this and I=92d be very grateful.****
>
>  ****
>
> Thanks.****
>
>  ****
>
> *From:* will trillich [mailto:will.trillich at serensoft.com]
> *Sent:* 25 April 2012 14:53
> *To:* The elegant MVC web framework
> *Subject:* Re: [Catalyst] TT via AJAX****
>
>  ****
>
> 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 via
> 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> [ $rs=
 ]
> )); =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 nor
> 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 receiv=
ed this e-mail in error, please notify the sender and destroy this e-mail a=
nd 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****
>
> 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 receiv=
ed this e-mail in error, please notify the sender and destroy this e-mail a=
nd 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****
>
> 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 receiv=
ed this e-mail in error, please notify the sender and destroy this e-mail a=
nd 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
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.scsys.co.uk/pipermail/catalyst/attachments/20120426/49c87=
645/attachment.htm


More information about the Catalyst mailing list