Login | Register For Free | Help
Search for: (Advanced)

Mailing List Archive: Catalyst: Users

TT via AJAX

 

 

Catalyst users RSS feed   Index | Next | Previous | View Threaded


rphilibert at aptina

Apr 25, 2012, 6:26 AM

Post #1 of 13 (841 views)
Permalink
TT via AJAX

Hello all,

I'd 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) = @_;

my $rs = $c->model(DN)->resultset(Table)->find(...);

my $body = $c->view(MYTT)->render($c,'dir/temp.mailtt',$c->stash( result => [ $rs ] )); this is the weird bit I guess.

my @content =();

push @content, $body;

$self->status_ok(

$c,

entity => \@content,

);

}

sub end :Private {

my ($self, $c) = @_;

$c->forward("View::JSON");

}



..but doing this I get the error:



Caught exception in MyApp::View::JSON->process "encountered object 'MyApp::Model::DB::Table=HASH(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 received this e-mail in error, please notify the sender and destroy this e-mail and any copies.


will.trillich at serensoft

Apr 25, 2012, 6:53 AM

Post #2 of 13 (826 views)
Permalink
Re: TT via AJAX [In reply to]

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 peril.

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 encoding
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 => [ $rs ] ) build your own data
structure, by doing something like

my $json = {
data => [],
count=> $rs->count,
other => $self->blah,
};
while ( my $rec = $rs->next ) {
my %fields = $self->get_important_values( $rec );
push @{ $json->{data} }, { %fields );
}
$c->stash( result => $json );



On Wed, Apr 25, 2012 at 8:26 AM, Roland Philibert <rphilibert [at] aptina>wrote:

> Hello all,****
>
> Id 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) = @_;****
>
> my $rs = $c->model(DN)->resultset(Table)->find(...);****
>
> my $body =
> $c->view(MYTT)->render($c,'dir/temp.mailtt',$c->stash( result => [ $rs ]
> )); this is the weird bit I guess.****
>
> my @content =();****
>
> push @content, $body;****
>
> $self->status_ok(****
>
> $c,****
>
> entity => \@content,****
>
> );****
>
> }****
>
> sub end :Private {****
>
> my ($self, $c) = @_;****
>
> $c->forward("View::JSON");****
>
> }****
>
> ** **
>
> ..but doing this I get the error:****
>
> ** **
>
> Caught exception in MyApp::View::JSON->process "encountered object
> 'MyApp::Model::DB::Table=HASH(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 received this e-mail in error, please notify the sender and destroy this e-mail and any copies.
>
>
>
> _______________________________________________
> List: Catalyst [at] lists
> Listinfo: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/catalyst
> Searchable archive:
> http://www.mail-archive.com/catalyst [at] lists/
> 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


mattp at cpan

Apr 25, 2012, 6:56 AM

Post #3 of 13 (827 views)
Permalink
Re: TT via AJAX [In reply to]

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>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 peril.
>
> 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 encoding
> 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 => [ $rs ] ) build your own data
> structure, by doing something like
>
> my $json = {
> data => [],
> count=> $rs->count,
> other => $self->blah,
> };
> while ( my $rec = $rs->next ) {
> my %fields = $self->get_important_values( $rec );
> push @{ $json->{data} }, { %fields );
> }
> $c->stash( result => $json );
>
>
>
> On Wed, Apr 25, 2012 at 8:26 AM, Roland Philibert <rphilibert [at] aptina>wrote:
>
>> Hello all,****
>>
>> Id 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) = @_;****
>>
>> my $rs = $c->model(DN)->resultset(Table)->find(...);****
>>
>> my $body =
>> $c->view(MYTT)->render($c,'dir/temp.mailtt',$c->stash( result => [ $rs ]
>> )); this is the weird bit I guess.****
>>
>> my @content =();****
>>
>> push @content, $body;****
>>
>> $self->status_ok(****
>>
>> $c,****
>>
>> entity => \@content,****
>>
>> );****
>>
>> }****
>>
>> sub end :Private {****
>>
>> my ($self, $c) = @_;****
>>
>> $c->forward("View::JSON");****
>>
>> }****
>>
>> ** **
>>
>> ..but doing this I get the error:****
>>
>> ** **
>>
>> Caught exception in MyApp::View::JSON->process "encountered object
>> 'MyApp::Model::DB::Table=HASH(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 received this e-mail in error, please notify the sender and destroy this e-mail and any copies.
>>
>>
>>
>> _______________________________________________
>> List: Catalyst [at] lists
>> Listinfo: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/catalyst
>> Searchable archive:
>> http://www.mail-archive.com/catalyst [at] lists/
>> 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
> Listinfo: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/catalyst
> Searchable archive:
> http://www.mail-archive.com/catalyst [at] lists/
> Dev site: http://dev.catalyst.perl.org/
>
>


rphilibert at aptina

Apr 26, 2012, 5:21 AM

Post #4 of 13 (818 views)
Permalink
RE: TT via AJAX [In reply to]

Hi Will,

Thanks for this, am not sure I understand though..

I don't 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'd be very grateful.



Thanks.



From: will trillich [mailto:will.trillich [at] serensoft]
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 peril.



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 encoding 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 => [ $rs ] ) build your own data structure, by doing something like



my $json = {

data => [],

count=> $rs->count,

other => $self->blah,

};

while ( my $rec = $rs->next ) {

my %fields = $self->get_important_values( $rec );

push @{ $json->{data} }, { %fields );

}

$c->stash( result => $json );







On Wed, Apr 25, 2012 at 8:26 AM, Roland Philibert <rphilibert [at] aptina> wrote:

Hello all,

I'd 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) = @_;

my $rs = $c->model(DN)->resultset(Table)->find(...);

my $body = $c->view(MYTT)->render($c,'dir/temp.mailtt',$c->stash( result => [ $rs ] )); this is the weird bit I guess.

my @content =();

push @content, $body;

$self->status_ok(

$c,

entity => \@content,

);

}

sub end :Private {

my ($self, $c) = @_;

$c->forward("View::JSON");

}



..but doing this I get the error:



Caught exception in MyApp::View::JSON->process "encountered object 'MyApp::Model::DB::Table=HASH(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 received this e-mail in error, please notify the sender and destroy this e-mail and any copies.



_______________________________________________
List: Catalyst [at] lists
Listinfo: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/catalyst
Searchable archive: http://www.mail-archive.com/catalyst [at] lists/
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 received this e-mail in error, please notify the sender and destroy this e-mail and any copies.


will.trillich at serensoft

Apr 26, 2012, 6:02 AM

Post #5 of 13 (823 views)
Permalink
Re: TT via AJAX [In reply to]

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 clutter
up your results (and take time to encode).

E.g.

DB<3> |x $rs
0 DBIx::Class::ResultSet=HASH(0xc1e8460)
'_result_class' => 'WT::Model::Model::Master'
'attrs' => HASH(0xc239ed8)
'alias' => 'me'
'where' => HASH(0xc1e55e0)
'ix' => '-1'
'cond' => HASH(0xc1e55e0)
-> REUSED_ADDRESS
'pager' => undef
'result_source' => DBIx::Class::ResultSource::Table=HASH(0xbbaa400)
'_columns' => HASH(0xbbaa7c0)
'cat' => HASH(0xbb74b30)
'data_type' => 'VARCHAR'
'default_value' => undef
'is_nullable' => 1
'size' => 8
'class' => HASH(0xbb74cb0)
'data_type' => 'VARCHAR'
'default_value' => undef
'is_nullable' => 1
'size' => 4
*...snip...300 or more lines later...snip...*
'sqlt_deploy_callback' => 'default_sqlt_deploy_hook'
'storage' => DBIx::Class::Storage::DBI=HASH(0xbba4470)
'_connect_info' => ARRAY(0xb9ae738)
0 HASH(0xac9f618)
'dsn' => 'dbi:mysql:dbname=wt'
'password' => 'sekrit-goeth-thither'
'user' => 'blahyaddablah'
'_dbh_details' => HASH(0xbba78b0)
empty hash
'_dbh_gen' => 0
'_dbi_connect_info' => ARRAY(0xab32940)
0 'dbi:mysql:dbname=wt'
1 'blahyaddablah'
2 'sekrit-goeth-thither'
3 HASH(0xab32860)
'AutoCommit' => 1
'PrintError' => 0
'RaiseError' => 1
'ShowErrorStatement' => 1
'_dbic_connect_attributes' => HASH(0xab32860)
-> REUSED_ADDRESS
'_in_dbh_do' => 0
'_sql_maker' => undef
'_sql_maker_opts' => HASH(0xbba78d0)
empty hash
'savepoints' => ARRAY(0xbba4480)
empty array
'schema' => WT::Schema=HASH(0xb93e380)
-> REUSED_ADDRESS
'transaction_depth' => 0
'source_name' => 'Master'
'sqlt_deploy_callback' => '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>wrote:

> Hi Will,****
>
> Thanks for this, am not sure I understand though..****
>
> I dont 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 Id be very grateful.****
>
> ** **
>
> Thanks.****
>
> ** **
>
> *From:* will trillich [mailto:will.trillich [at] serensoft]
> *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 peril.
> ****
>
> ** **
>
> 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 encoding
> 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 => [ $rs ] ) build your own data
> structure, by doing something like****
>
> ** **
>
> my $json = {****
>
> data => [],****
>
> count=> $rs->count,****
>
> other => $self->blah,****
>
> };****
>
> while ( my $rec = $rs->next ) {****
>
> my %fields = $self->get_important_values( $rec );****
>
> push @{ $json->{data} }, { %fields );****
>
> }****
>
> $c->stash( result => $json );****
>
> ** **
>
> ** **
>
> ** **
>
> On Wed, Apr 25, 2012 at 8:26 AM, Roland Philibert <rphilibert [at] aptina>
> wrote:****
>
> Hello all,****
>
> Id 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) = @_;****
>
> my $rs = $c->model(DN)->resultset(Table)->find(...);****
>
> my $body =
> $c->view(MYTT)->render($c,'dir/temp.mailtt',$c->stash( result => [ $rs ]
> )); this is the weird bit I guess.****
>
> my @content =();****
>
> push @content, $body;****
>
> $self->status_ok(****
>
> $c,****
>
> entity => \@content,****
>
> );****
>
> }****
>
> sub end :Private {****
>
> my ($self, $c) = @_;****
>
> $c->forward("View::JSON");****
>
> }****
>
> ****
>
> ..but doing this I get the error:****
>
> ****
>
> Caught exception in MyApp::View::JSON->process "encountered object
> 'MyApp::Model::DB::Table=HASH(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 received this e-mail in error, please notify the sender and destroy this e-mail and any copies.****
>
> ** **
>
>
> _______________________________________________
> List: Catalyst [at] lists
> Listinfo: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/catalyst
> Searchable archive:
> http://www.mail-archive.com/catalyst [at] lists/
> 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 received this e-mail in error, please notify the sender and destroy this e-mail and any copies.
>
>
>
> _______________________________________________
> List: Catalyst [at] lists
> Listinfo: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/catalyst
> Searchable archive:
> http://www.mail-archive.com/catalyst [at] lists/
> 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


lists at eightdegrees

Apr 26, 2012, 6:31 AM

Post #6 of 13 (817 views)
Permalink
Re: TT via AJAX [In reply to]

Hi Roland,

On Wed, Apr 25, 2012 at 11:26 PM, Roland Philibert <rphilibert [at] aptina>wrote:

> Hello all,****
>
> Id 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) = @_;****
>
> my $rs = $c->model(DN)->resultset(Table)->find(...);****
>
> my $body =
> $c->view(MYTT)->render($c,'dir/temp.mailtt',$c->stash( result => [ $rs ]
> )); this is the weird bit I guess.****
>
> my @content =();****
>
> push @content, $body;****
>
> $self->status_ok(****
>
> $c,****
>
> entity => \@content,****
>
> );****
>
> }****
>
> sub end :Private {****
>
> my ($self, $c) = @_;****
>
> $c->forward("View::JSON");****
>
> }****
>
> ** **
>
> ..but doing this I get the error:****
>
> ** **
>
> Caught exception in MyApp::View::JSON->process "encountered object
> 'MyApp::Model::DB::Table=HASH(0xdb2c330)', but neither allow_blessed nor
> convert_blessed settings are enabled at (eval 1606) line 151."
>

have you tried this without the 'end' action?

What is MyApp::View::JSON trying to serialize? or what module are you
subclassing?

Looks to me like $body should be what you think it is (have you confirmed
that?) but I don't think MyApp::View::JSON is serializing your content
array, but instead is attempting to serialize the "result" in your stash.

cheers,

J


> ****
>
> ** **
>
> 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 received this e-mail in error, please notify the sender and destroy this e-mail and any copies.
>
>
>
> _______________________________________________
> List: Catalyst [at] lists
> Listinfo: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/catalyst
> Searchable archive:
> http://www.mail-archive.com/catalyst [at] lists/
> Dev site: http://dev.catalyst.perl.org/
>
>


rphilibert at aptina

Apr 26, 2012, 7:03 AM

Post #7 of 13 (821 views)
Permalink
RE: TT via AJAX [In reply to]

Hi Jason.



If I remove the end action and wite $body in the log, I do get what I want (ie rendered html from my TT with its "result" variable).

...so your conclusion about not serializing the @content seems to be on the right track.
...is this liked to what Will has suggested to get around this issue? What do you suggest I do next?



Thanks

R.



From: Jason Galea [mailto:lists [at] eightdegrees]
Sent: 26 April 2012 14:32
To: The elegant MVC web framework
Subject: Re: [Catalyst] TT via AJAX





Hi Roland,



On Wed, Apr 25, 2012 at 11:26 PM, Roland Philibert <rphilibert [at] aptina> wrote:

Hello all,

I'd 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) = @_;

my $rs = $c->model(DN)->resultset(Table)->find(...);

my $body = $c->view(MYTT)->render($c,'dir/temp.mailtt',$c->stash( result => [ $rs ] )); this is the weird bit I guess.

my @content =();

push @content, $body;

$self->status_ok(

$c,

entity => \@content,

);

}

sub end :Private {

my ($self, $c) = @_;

$c->forward("View::JSON");

}



..but doing this I get the error:



Caught exception in MyApp::View::JSON->process "encountered object 'MyApp::Model::DB::Table=HASH(0xdb2c330)', but neither allow_blessed nor convert_blessed settings are enabled at (eval 1606) line 151."



have you tried this without the 'end' action?



What is MyApp::View::JSON trying to serialize? or what module are you subclassing?



Looks to me like $body should be what you think it is (have you confirmed that?) but I don't think MyApp::View::JSON is serializing your content array, but instead is attempting to serialize the "result" in your stash.



cheers,



J





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 received this e-mail in error, please notify the sender and destroy this e-mail and any copies.



_______________________________________________
List: Catalyst [at] lists
Listinfo: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/catalyst
Searchable archive: http://www.mail-archive.com/catalyst [at] lists/
Dev site: http://dev.catalyst.perl.org/




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 received this e-mail in error, please notify the sender and destroy this e-mail and any copies.


rphilibert at aptina

Apr 26, 2012, 12:24 PM

Post #8 of 13 (819 views)
Permalink
RE: TT via AJAX [In reply to]

Hi Will,

In your previous suggestion you had:



while ( my $rec = $rs->next ) {

my %fields = $self->get_important_values( $rec );

push @{ $json->{data} }, { %fields );

}



...what is "get_important_values"? How to make sure that all the resultset is transferred to $json.



Thanks for you help.

R







From: will trillich [mailto:will.trillich [at] serensoft]
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 clutter up your results (and take time to encode).



E.g.



DB<3> |x $rs

0 DBIx::Class::ResultSet=HASH(0xc1e8460)

'_result_class' => 'WT::Model::Model::Master'

'attrs' => HASH(0xc239ed8)

'alias' => 'me'

'where' => HASH(0xc1e55e0)

'ix' => '-1'

'cond' => HASH(0xc1e55e0)

-> REUSED_ADDRESS

'pager' => undef

'result_source' => DBIx::Class::ResultSource::Table=HASH(0xbbaa400)

'_columns' => HASH(0xbbaa7c0)

'cat' => HASH(0xbb74b30)

'data_type' => 'VARCHAR'

'default_value' => undef

'is_nullable' => 1

'size' => 8

'class' => HASH(0xbb74cb0)

'data_type' => 'VARCHAR'

'default_value' => undef

'is_nullable' => 1

'size' => 4

...snip...300 or more lines later...snip...

'sqlt_deploy_callback' => 'default_sqlt_deploy_hook'

'storage' => DBIx::Class::Storage::DBI=HASH(0xbba4470)

'_connect_info' => ARRAY(0xb9ae738)

0 HASH(0xac9f618)

'dsn' => 'dbi:mysql:dbname=wt'

'password' => 'sekrit-goeth-thither'

'user' => 'blahyaddablah'

'_dbh_details' => HASH(0xbba78b0)

empty hash

'_dbh_gen' => 0

'_dbi_connect_info' => ARRAY(0xab32940)

0 'dbi:mysql:dbname=wt'

1 'blahyaddablah'

2 'sekrit-goeth-thither'

3 HASH(0xab32860)

'AutoCommit' => 1

'PrintError' => 0

'RaiseError' => 1

'ShowErrorStatement' => 1

'_dbic_connect_attributes' => HASH(0xab32860)

-> REUSED_ADDRESS

'_in_dbh_do' => 0

'_sql_maker' => undef

'_sql_maker_opts' => HASH(0xbba78d0)

empty hash

'savepoints' => ARRAY(0xbba4480)

empty array

'schema' => WT::Schema=HASH(0xb93e380)

-> REUSED_ADDRESS

'transaction_depth' => 0

'source_name' => 'Master'

'sqlt_deploy_callback' => '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> wrote:

Hi Will,

Thanks for this, am not sure I understand though..

I don't 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'd be very grateful.



Thanks.



From: will trillich [mailto:will.trillich [at] serensoft]
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 peril.



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 encoding 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 => [ $rs ] ) build your own data structure, by doing something like



my $json = {

data => [],

count=> $rs->count,

other => $self->blah,

};

while ( my $rec = $rs->next ) {

my %fields = $self->get_important_values( $rec );

push @{ $json->{data} }, { %fields );

}

$c->stash( result => $json );







On Wed, Apr 25, 2012 at 8:26 AM, Roland Philibert <rphilibert [at] aptina> wrote:

Hello all,

I'd 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) = @_;

my $rs = $c->model(DN)->resultset(Table)->find(...);

my $body = $c->view(MYTT)->render($c,'dir/temp.mailtt',$c->stash( result => [ $rs ] )); this is the weird bit I guess.

my @content =();

push @content, $body;

$self->status_ok(

$c,

entity => \@content,

);

}

sub end :Private {

my ($self, $c) = @_;

$c->forward("View::JSON");

}



..but doing this I get the error:



Caught exception in MyApp::View::JSON->process "encountered object 'MyApp::Model::DB::Table=HASH(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 received this e-mail in error, please notify the sender and destroy this e-mail and any copies.



_______________________________________________
List: Catalyst [at] lists
Listinfo: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/catalyst
Searchable archive: http://www.mail-archive.com/catalyst [at] lists/
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 received this e-mail in error, please notify the sender and destroy this e-mail and any copies.



_______________________________________________
List: Catalyst [at] lists
Listinfo: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/catalyst
Searchable archive: http://www.mail-archive.com/catalyst [at] lists/
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 received this e-mail in error, please notify the sender and destroy this e-mail and any copies.


will.trillich at serensoft

Apr 26, 2012, 1:26 PM

Post #9 of 13 (817 views)
Permalink
Re: TT via AJAX [In reply to]

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 = 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 = shift;
my $record = shift;

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

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

On Thu, Apr 26, 2012 at 2:24 PM, Roland Philibert <rphilibert [at] aptina>wrote:

> Hi Will,****
>
> In your previous suggestion you had:****
>
> ** **
>
> while ( my $rec = $rs->next ) {****
>
> my %fields = $self->get_important_values(
> $rec );****
>
> push @{ $json->{data} }, { %fields );****
>
> }****
>
> ** **
>
> what is get_important_values? How to make sure that all the resultset
> is transferred to $json.****
>
> ** **
>
> Thanks for you help.****
>
> R****
>
> ** **
>
> ** **
>
> ** **
>
> *From:* will trillich [mailto:will.trillich [at] serensoft]
> *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 clutter
> up your results (and take time to encode).****
>
> ** **
>
> E.g.****
>
> ** **
>
> DB<3> |x $rs****
>
> 0 DBIx::Class::ResultSet=HASH(0xc1e8460)****
>
> '_result_class' => 'WT::Model::Model::Master'****
>
> 'attrs' => HASH(0xc239ed8)****
>
> 'alias' => 'me'****
>
> 'where' => HASH(0xc1e55e0)****
>
> 'ix' => '-1'****
>
> 'cond' => HASH(0xc1e55e0)****
>
> -> REUSED_ADDRESS****
>
> 'pager' => undef****
>
> 'result_source' => DBIx::Class::ResultSource::Table=HASH(0xbbaa400)****
>
> '_columns' => HASH(0xbbaa7c0)****
>
> 'cat' => HASH(0xbb74b30)****
>
> 'data_type' => 'VARCHAR'****
>
> 'default_value' => undef****
>
> 'is_nullable' => 1****
>
> 'size' => 8****
>
> 'class' => HASH(0xbb74cb0)****
>
> 'data_type' => 'VARCHAR'****
>
> 'default_value' => undef****
>
> 'is_nullable' => 1****
>
> 'size' => 4****
>
> *...snip...300 or more lines later...snip...*****
>
> 'sqlt_deploy_callback' => 'default_sqlt_deploy_hook'****
>
> 'storage' => DBIx::Class::Storage::DBI=HASH(0xbba4470)****
>
> '_connect_info' => ARRAY(0xb9ae738)****
>
> 0 HASH(0xac9f618)****
>
> 'dsn' => 'dbi:mysql:dbname=wt'****
>
> 'password' => 'sekrit-goeth-thither'****
>
> 'user' => 'blahyaddablah'****
>
> '_dbh_details' => HASH(0xbba78b0)****
>
> empty hash****
>
> '_dbh_gen' => 0****
>
> '_dbi_connect_info' => ARRAY(0xab32940)****
>
> 0 'dbi:mysql:dbname=wt'****
>
> 1 'blahyaddablah'****
>
> 2 'sekrit-goeth-thither'****
>
> 3 HASH(0xab32860)****
>
> 'AutoCommit' => 1****
>
> 'PrintError' => 0****
>
> 'RaiseError' => 1****
>
> 'ShowErrorStatement' => 1****
>
> '_dbic_connect_attributes' => HASH(0xab32860)****
>
> -> REUSED_ADDRESS****
>
> '_in_dbh_do' => 0****
>
> '_sql_maker' => undef****
>
> '_sql_maker_opts' => HASH(0xbba78d0)****
>
> empty hash****
>
> 'savepoints' => ARRAY(0xbba4480)****
>
> empty array****
>
> 'schema' => WT::Schema=HASH(0xb93e380)****
>
> -> REUSED_ADDRESS****
>
> 'transaction_depth' => 0****
>
> 'source_name' => 'Master'****
>
> 'sqlt_deploy_callback' => '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>
> wrote:****
>
> Hi Will,****
>
> Thanks for this, am not sure I understand though..****
>
> I dont 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 Id be very grateful.****
>
> ****
>
> Thanks.****
>
> ****
>
> *From:* will trillich [mailto:will.trillich [at] serensoft]
> *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 peril.
> ****
>
> ****
>
> 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 encoding
> 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 => [ $rs ] ) build your own data
> structure, by doing something like****
>
> ****
>
> my $json = {****
>
> data => [],****
>
> count=> $rs->count,****
>
> other => $self->blah,****
>
> };****
>
> while ( my $rec = $rs->next ) {****
>
> my %fields = $self->get_important_values( $rec );****
>
> push @{ $json->{data} }, { %fields );****
>
> }****
>
> $c->stash( result => $json );****
>
> ****
>
> ****
>
> ****
>
> On Wed, Apr 25, 2012 at 8:26 AM, Roland Philibert <rphilibert [at] aptina>
> wrote:****
>
> Hello all,****
>
> Id 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) = @_;****
>
> my $rs = $c->model(DN)->resultset(Table)->find(...);****
>
> my $body =
> $c->view(MYTT)->render($c,'dir/temp.mailtt',$c->stash( result => [ $rs ]
> )); this is the weird bit I guess.****
>
> my @content =();****
>
> push @content, $body;****
>
> $self->status_ok(****
>
> $c,****
>
> entity => \@content,****
>
> );****
>
> }****
>
> sub end :Private {****
>
> my ($self, $c) = @_;****
>
> $c->forward("View::JSON");****
>
> }****
>
> ****
>
> ..but doing this I get the error:****
>
> ****
>
> Caught exception in MyApp::View::JSON->process "encountered object
> 'MyApp::Model::DB::Table=HASH(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 received this e-mail in error, please notify the sender and destroy this e-mail and any copies.****
>
> ****
>
>
> _______________________________________________
> List: Catalyst [at] lists
> Listinfo: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/catalyst
> Searchable archive:
> http://www.mail-archive.com/catalyst [at] lists/
> 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 received this e-mail in error, please notify the sender and destroy this e-mail and any copies.****
>
> ** **
>
>
> _______________________________________________
> List: Catalyst [at] lists
> Listinfo: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/catalyst
> Searchable archive:
> http://www.mail-archive.com/catalyst [at] lists/
> 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 received this e-mail in error, please notify the sender and destroy this e-mail and any copies.
>
>
>
> _______________________________________________
> List: Catalyst [at] lists
> Listinfo: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/catalyst
> Searchable archive:
> http://www.mail-archive.com/catalyst [at] lists/
> 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


lists at eightdegrees

Apr 26, 2012, 6:51 PM

Post #10 of 13 (816 views)
Permalink
Re: TT via AJAX [In reply to]

On Fri, Apr 27, 2012 at 12:03 AM, Roland Philibert <rphilibert [at] aptina>wrote:

> If I remove the end action and wite $body in the log, I do get what I want
> (ie rendered html from my TT with its result variable).
>
> so your conclusion about not serializing the @content seems to be on the
> right track.
> is this liked to what Will has suggested to get around this issue? What
> do you suggest I do next?
>

well, now a GET request to your ajaxaction action (with content-type
"application/json") should return the request body containing a json array
with the first (and only) element being your html.


> ****
>
> ** **
>
> Thanks****
>
> R.****
>
> ** **
>
> *From:* Jason Galea [mailto:lists [at] eightdegrees]
> *Sent:* 26 April 2012 14:32
>
> *To:* The elegant MVC web framework
> *Subject:* Re: [Catalyst] TT via AJAX****
>
> ** **
>
> ** **
>
> Hi Roland,****
>
> ** **
>
> On Wed, Apr 25, 2012 at 11:26 PM, Roland Philibert <rphilibert [at] aptina>
> wrote:****
>
> Hello all,****
>
> Id 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) = @_;****
>
> my $rs = $c->model(DN)->resultset(Table)->find(...);****
>
> my $body =
> $c->view(MYTT)->render($c,'dir/temp.mailtt',$c->stash( result => [ $rs ]
> )); this is the weird bit I guess.****
>
> my @content =();****
>
> push @content, $body;****
>
> $self->status_ok(****
>
> $c,****
>
> entity => \@content,****
>
> );****
>
> }****
>
> sub end :Private {****
>
> my ($self, $c) = @_;****
>
> $c->forward("View::JSON");****
>
> }****
>
> ****
>
> ..but doing this I get the error:****
>
> ****
>
> Caught exception in MyApp::View::JSON->process "encountered object
> 'MyApp::Model::DB::Table=HASH(0xdb2c330)', but neither allow_blessed nor
> convert_blessed settings are enabled at (eval 1606) line 151."****
>
> ** **
>
> have you tried this without the 'end' action?****
>
> ** **
>
> What is MyApp::View::JSON trying to serialize? or what module are you
> subclassing?****
>
> ** **
>
> Looks to me like $body should be what you think it is (have you confirmed
> that?) but I don't think MyApp::View::JSON is serializing your content
> array, but instead is attempting to serialize the "result" in your stash.*
> ***
>
> ** **
>
> cheers,****
>
> ** **
>
> J****
>
> ****
>
> ****
>
> 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 received this e-mail in error, please notify the sender and destroy this e-mail and any copies.****
>
> ** **
>
>
> _______________________________________________
> List: Catalyst [at] lists
> Listinfo: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/catalyst
> Searchable archive:
> http://www.mail-archive.com/catalyst [at] lists/
> Dev site: http://dev.catalyst.perl.org/****
>
> ** **
>
> 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 received this e-mail in error, please notify the sender and destroy this e-mail and any copies.
>
>
>
> _______________________________________________
> List: Catalyst [at] lists
> Listinfo: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/catalyst
> Searchable archive:
> http://www.mail-archive.com/catalyst [at] lists/
> Dev site: http://dev.catalyst.perl.org/
>
>


rphilibert at aptina

Apr 27, 2012, 4:54 AM

Post #11 of 13 (813 views)
Permalink
RE: TT via AJAX [In reply to]

Hi Jason,



My ajax action are already set as you say.

I still need to forward to a JSON view, which was in my "end" action. If I dont forward it, I don't get anything back at the client..but if I do then back to square one..





From: Jason Galea [mailto:lists [at] eightdegrees]
Sent: 27 April 2012 02:52
To: The elegant MVC web framework
Subject: Re: [Catalyst] TT via AJAX





On Fri, Apr 27, 2012 at 12:03 AM, Roland Philibert <rphilibert [at] aptina> wrote:

If I remove the end action and wite $body in the log, I do get what I want (ie rendered html from my TT with its "result" variable).

...so your conclusion about not serializing the @content seems to be on the right track.
...is this liked to what Will has suggested to get around this issue? What do you suggest I do next?



well, now a GET request to your ajaxaction action (with content-type "application/json") should return the request body containing a json array with the first (and only) element being your html.





Thanks

R.



From: Jason Galea [mailto:lists [at] eightdegrees]
Sent: 26 April 2012 14:32


To: The elegant MVC web framework
Subject: Re: [Catalyst] TT via AJAX





Hi Roland,



On Wed, Apr 25, 2012 at 11:26 PM, Roland Philibert <rphilibert [at] aptina> wrote:

Hello all,

I'd 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) = @_;

my $rs = $c->model(DN)->resultset(Table)->find(...);

my $body = $c->view(MYTT)->render($c,'dir/temp.mailtt',$c->stash( result => [ $rs ] )); this is the weird bit I guess.

my @content =();

push @content, $body;

$self->status_ok(

$c,

entity => \@content,

);

}

sub end :Private {

my ($self, $c) = @_;

$c->forward("View::JSON");

}



..but doing this I get the error:



Caught exception in MyApp::View::JSON->process "encountered object 'MyApp::Model::DB::Table=HASH(0xdb2c330)', but neither allow_blessed nor convert_blessed settings are enabled at (eval 1606) line 151."



have you tried this without the 'end' action?



What is MyApp::View::JSON trying to serialize? or what module are you subclassing?



Looks to me like $body should be what you think it is (have you confirmed that?) but I don't think MyApp::View::JSON is serializing your content array, but instead is attempting to serialize the "result" in your stash.



cheers,



J





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 received this e-mail in error, please notify the sender and destroy this e-mail and any copies.



_______________________________________________
List: Catalyst [at] lists
Listinfo: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/catalyst
Searchable archive: http://www.mail-archive.com/catalyst [at] lists/
Dev site: http://dev.catalyst.perl.org/



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 received this e-mail in error, please notify the sender and destroy this e-mail and any copies.



_______________________________________________
List: Catalyst [at] lists
Listinfo: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/catalyst
Searchable archive: http://www.mail-archive.com/catalyst [at] lists/
Dev site: http://dev.catalyst.perl.org/




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 received this e-mail in error, please notify the sender and destroy this e-mail and any copies.


lists at eightdegrees

Apr 27, 2012, 5:29 AM

Post #12 of 13 (810 views)
Permalink
Re: TT via AJAX [In reply to]

Hi Roland,

you shouldn't need to forward to a JSON view. Calling the status_ok method
should set up the response and return the serialized data. Is there
anything showing in the error logs? What client are you using to test?

there isn't really a space between "sub ajaxaction" & "_GET {" in your
source is there?

J

On Fri, Apr 27, 2012 at 9:54 PM, Roland Philibert <rphilibert [at] aptina>wrote:

> Hi Jason,****
>
> ** **
>
> My ajax action are already set as you say. ****
>
> I still need to forward to a JSON view, which was in my end action. If I
> dont forward it, I dont get anything back at the client..but if I do then
> back to square one..****
>
> ** **
>
> ** **
>
> *From:* Jason Galea [mailto:lists [at] eightdegrees]
> *Sent:* 27 April 2012 02:52
>
> *To:* The elegant MVC web framework
> *Subject:* Re: [Catalyst] TT via AJAX****
>
> ** **
>
> ** **
>
> On Fri, Apr 27, 2012 at 12:03 AM, Roland Philibert <rphilibert [at] aptina>
> wrote:****
>
> If I remove the end action and wite $body in the log, I do get what I want
> (ie rendered html from my TT with its result variable).****
>
> so your conclusion about not serializing the @content seems to be on the
> right track.
> is this liked to what Will has suggested to get around this issue? What
> do you suggest I do next?****
>
> ** **
>
> well, now a GET request to your ajaxaction action (with content-type
> "application/json") should return the request body containing a json array
> with the first (and only) element being your html.****
>
> ****
>
> ****
>
> Thanks****
>
> R.****
>
> ****
>
> *From:* Jason Galea [mailto:lists [at] eightdegrees]
> *Sent:* 26 April 2012 14:32****
>
>
> *To:* The elegant MVC web framework
> *Subject:* Re: [Catalyst] TT via AJAX****
>
> ****
>
> ****
>
> Hi Roland,****
>
> ****
>
> On Wed, Apr 25, 2012 at 11:26 PM, Roland Philibert <rphilibert [at] aptina>
> wrote:****
>
> Hello all,****
>
> Id 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) = @_;****
>
> my $rs = $c->model(DN)->resultset(Table)->find(...);****
>
> my $body =
> $c->view(MYTT)->render($c,'dir/temp.mailtt',$c->stash( result => [ $rs ]
> )); this is the weird bit I guess.****
>
> my @content =();****
>
> push @content, $body;****
>
> $self->status_ok(****
>
> $c,****
>
> entity => \@content,****
>
> );****
>
> }****
>
> sub end :Private {****
>
> my ($self, $c) = @_;****
>
> $c->forward("View::JSON");****
>
> }****
>
> ****
>
> ..but doing this I get the error:****
>
> ****
>
> Caught exception in MyApp::View::JSON->process "encountered object
> 'MyApp::Model::DB::Table=HASH(0xdb2c330)', but neither allow_blessed nor
> convert_blessed settings are enabled at (eval 1606) line 151."****
>
> ****
>
> have you tried this without the 'end' action?****
>
> ****
>
> What is MyApp::View::JSON trying to serialize? or what module are you
> subclassing?****
>
> ****
>
> Looks to me like $body should be what you think it is (have you confirmed
> that?) but I don't think MyApp::View::JSON is serializing your content
> array, but instead is attempting to serialize the "result" in your stash.*
> ***
>
> ****
>
> cheers,****
>
> ****
>
> J****
>
> ****
>
> ****
>
> 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 received this e-mail in error, please notify the sender and destroy this e-mail and any copies.****
>
> ****
>
>
> _______________________________________________
> List: Catalyst [at] lists
> Listinfo: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/catalyst
> Searchable archive:
> http://www.mail-archive.com/catalyst [at] lists/
> Dev site: http://dev.catalyst.perl.org/****
>
> ****
>
> 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 received this e-mail in error, please notify the sender and destroy this e-mail and any copies.****
>
> ** **
>
>
> _______________________________________________
> List: Catalyst [at] lists
> Listinfo: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/catalyst
> Searchable archive:
> http://www.mail-archive.com/catalyst [at] lists/
> Dev site: http://dev.catalyst.perl.org/****
>
> ** **
>
> 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 received this e-mail in error, please notify the sender and destroy this e-mail and any copies.
>
>
>
> _______________________________________________
> List: Catalyst [at] lists
> Listinfo: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/catalyst
> Searchable archive:
> http://www.mail-archive.com/catalyst [at] lists/
> Dev site: http://dev.catalyst.perl.org/
>
>


rphilibert at aptina

Apr 27, 2012, 8:15 AM

Post #13 of 13 (815 views)
Permalink
RE: TT via AJAX [In reply to]

Hi Jason,

No, nothing there...I'll try to make a simple testcase.

Thanks

Roland



From: Jason Galea [mailto:lists [at] eightdegrees]
Sent: 27 April 2012 13:29
To: The elegant MVC web framework
Subject: Re: [Catalyst] TT via AJAX



Hi Roland,



you shouldn't need to forward to a JSON view. Calling the status_ok method should set up the response and return the serialized data. Is there anything showing in the error logs? What client are you using to test?



there isn't really a space between "sub ajaxaction" & "_GET {" in your source is there?



J

On Fri, Apr 27, 2012 at 9:54 PM, Roland Philibert <rphilibert [at] aptina> wrote:

Hi Jason,



My ajax action are already set as you say.

I still need to forward to a JSON view, which was in my "end" action. If I dont forward it, I don't get anything back at the client..but if I do then back to square one..





From: Jason Galea [mailto:lists [at] eightdegrees]
Sent: 27 April 2012 02:52


To: The elegant MVC web framework
Subject: Re: [Catalyst] TT via AJAX





On Fri, Apr 27, 2012 at 12:03 AM, Roland Philibert <rphilibert [at] aptina> wrote:

If I remove the end action and wite $body in the log, I do get what I want (ie rendered html from my TT with its "result" variable).

...so your conclusion about not serializing the @content seems to be on the right track.
...is this liked to what Will has suggested to get around this issue? What do you suggest I do next?



well, now a GET request to your ajaxaction action (with content-type "application/json") should return the request body containing a json array with the first (and only) element being your html.





Thanks

R.



From: Jason Galea [mailto:lists [at] eightdegrees]
Sent: 26 April 2012 14:32


To: The elegant MVC web framework
Subject: Re: [Catalyst] TT via AJAX





Hi Roland,



On Wed, Apr 25, 2012 at 11:26 PM, Roland Philibert <rphilibert [at] aptina> wrote:

Hello all,

I'd 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) = @_;

my $rs = $c->model(DN)->resultset(Table)->find(...);

my $body = $c->view(MYTT)->render($c,'dir/temp.mailtt',$c->stash( result => [ $rs ] )); this is the weird bit I guess.

my @content =();

push @content, $body;

$self->status_ok(

$c,

entity => \@content,

);

}

sub end :Private {

my ($self, $c) = @_;

$c->forward("View::JSON");

}



..but doing this I get the error:



Caught exception in MyApp::View::JSON->process "encountered object 'MyApp::Model::DB::Table=HASH(0xdb2c330)', but neither allow_blessed nor convert_blessed settings are enabled at (eval 1606) line 151."



have you tried this without the 'end' action?



What is MyApp::View::JSON trying to serialize? or what module are you subclassing?



Looks to me like $body should be what you think it is (have you confirmed that?) but I don't think MyApp::View::JSON is serializing your content array, but instead is attempting to serialize the "result" in your stash.



cheers,



J





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 received this e-mail in error, please notify the sender and destroy this e-mail and any copies.



_______________________________________________
List: Catalyst [at] lists
Listinfo: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/catalyst
Searchable archive: http://www.mail-archive.com/catalyst [at] lists/
Dev site: http://dev.catalyst.perl.org/



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 received this e-mail in error, please notify the sender and destroy this e-mail and any copies.



_______________________________________________
List: Catalyst [at] lists
Listinfo: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/catalyst
Searchable archive: http://www.mail-archive.com/catalyst [at] lists/
Dev site: http://dev.catalyst.perl.org/



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 received this e-mail in error, please notify the sender and destroy this e-mail and any copies.



_______________________________________________
List: Catalyst [at] lists
Listinfo: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/catalyst
Searchable archive: http://www.mail-archive.com/catalyst [at] lists/
Dev site: http://dev.catalyst.perl.org/




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 received this e-mail in error, please notify the sender and destroy this e-mail and any copies.

Catalyst users RSS feed   Index | Next | Previous | View Threaded
 
 


Interested in having your list archived? Contact Gossamer Threads
 
  Web Applications & Managed Hosting Powered by Gossamer Threads Inc.