[Catalyst] $c->res->body vs $c->forward(Myapp::V::Mason)

Kevin Old kevinold at gmail.com
Wed Sep 21 18:17:00 CEST 2005


Hello everyone,

I'm generating some data for output in an Ajax call using
HTML::Prototype's link_to_remote.

Here's my Controller:

sub listvisits : Regex('^visits/listvisits/([0-9]+)$') {
    my ( $self, $c ) = @_;

    if ( $c->roles(qw/EMP/) ) {
        $c->stash->{VISITS} = IVR::M::Visits->search_visits_emp(
            $c->session->{user},
            $c->req->snippets->[0],
            $c->req->snippets->[0],
        );
    }
    else {
        $c->stash->{VISITS} = IVR::M::Visits->search_visits_sup(
            $c->session->{user},
            $c->req->params->{body},
            $c->req->params->{body},
        );
    }

   if ( $c->req->param('xls') ) {
        my @visits
            = map { $_ } $c->stash->{VISITS}->data;
        $c->log->_dump(@visits);
        $c->stash->{VISITS}   = \@visits;
        $c->stash->{template} = 'visits.xml';
        $c->forward('IVR::V::Excel::Template');
    }
    else {
        $c->stash->{template} = 'comps/visits_tbl_test.mas';
        $c->forward('IVR::V::Mason');
    }

}

Here's my template ( visits_tbl_test.mas):

<%args>
$VISITS
</%args>

<ul>
<%perl>

while ( my $k = $VISITS->next ) {
    $c->log->warn('dte: ' . $k->datetimeentered);
    print '<li>dte: ' . $k->datetimeentered . '</li>';
}

</%perl>
</ul>

<a href="/visits/listvisits?body=361700&xls=1"><img
src="/i/page_excel.png">Download to Excel</a>
<%flags>
inherit => undef
</%flags>


Here's my template code to generate the ajax stuff:

<%perl>

my $loading = $c->prototype->update_element_function( 'acomp_stat', {
action => 'update', position => 'bottom', content => 'Loading
Users...' } );
my $complete .= $c->prototype->update_element_function( 'acomp_stat',
{ action => 'update', content => '' } );

    print $c->prototype->link_to_remote( "freds", { url =>
"/visits/listvisits/371200", loading => "$loading", update => 'users',
complete => "$complete" } );

</%perl>
        <span id="acomp_stat"></span><br />
    <div id="users"></div><br/>

First, everything works perfect in Firefox, but when I execute it in
IE I get Javascript errors in Ajax.Updater.  I know this code can work
in IE as I have another Controller that rather than passing the data
from the SQL query to a view, it generates the HTML to be displayed
and calls $c->res->body and everything works perfect in IE and
Firefox.

Here's that code:

sub emplistajax : Regex('^emplistajax/([A-Z])$') {
    my ( $self, $c ) = @_;
    $c->log->_dump( $c->req->snippets );
    my $likeln = $c->req->snippets->[0] . '%';
    my @tmp    = IVR::M::EmpDetails->search_emps_srt_ln2($likeln);
    my @items;
    for my $usr (@tmp) {
        my $link = '/emp.html/' . $usr->employeeid;
        my $name = $usr->lastname . ", " . $usr->firstname;

        #push @items, HTML::Element->new('a', "$link");
        push @items,
            HTML::Element->new( 'a', href => "$link" )->push_content($name);
    }
    my @elements;
    for my $item (@items) {
        push @elements, HTML::Element->new('li')->push_content($item);
    }
    $c->res->body(
        HTML::Element->new('ul')->push_content(@elements)->as_HTML );
}


My initial thoughts are to change my listvisits code around to work
like the other, but was wondering if there might be some other
underlying problem that I'm not aware of.

Are there differences with using the $c->res->body vs
$c->forward(Myapp::V::Mason) with the stuff in HTML::Prototype?

Anyone else run into stuff like this?

Thanks for any help!
Kevin
--
Kevin Old
kevinold at gmail.com



More information about the Catalyst mailing list