FAQ
Hello list

Both ajax and non-ajax actions chain off the same action
(base_with_id). If an error occurs (like the resource doesn't exist) I
detach to '/error404' in my Root Controller.
I just introduced the ajax stuff and before that 'error404' simply
rendered an error template. Now I'd like to check if it is an xmlhttp
request using http://search.cpan.org/~flora/Catalyst-TraitFor-Request-XMLHttpRequest-0.01/
and then either render the error template like before OR use the
status_not_found() helper from
http://search.cpan.org/~bobtfish/Catalyst-Action-REST-0.87/

To use status_not_found I need to extend my Root Controller from
Catalyst::Controller::REST

That'd mean i have to check the type of request again in my roots end
method to avoid rendering of the template.

#Root.pm
sub render : ActionClass('RenderView') {}
sub end :Private {
my ($self, $c) = @_;
unless ($c->request->is_xhr) {
$c->forward('render');
}
}


Any comments on that solution are appreciated as are pointers to alternatives.

david


#User.pm
sub base : Chained('') PathPart('') CaptureArgs(0) {
my ($self, $c ) = @_;
$c->stash(users_rs => $c->model('DB::Users'));
}

sub base_with_id : Chained('base') PathPart('') CaptureArgs(1) {
my ($self, $c, $id ) = @_;
my $user = $c->stash->{'users_rs'}->find($id);
if ($user) {
$c->stash(user = $user);
} else {
$c->stash(error_msg => 'User not found.');
$c->detach('/error404');
}
}

Search Discussions

  • Eden Cardim at Nov 9, 2010 at 10:38 am
    "David" == David Schmidt writes:
    David> Hello list Both ajax and non-ajax actions chain off the same
    David> action (base_with_id). If an error occurs (like the resource
    David> doesn't exist) I detach to '/error404' in my Root Controller.
    David> I just introduced the ajax stuff and before that 'error404'
    David> simply rendered an error template. Now I'd like to check if
    David> it is an xmlhttp request using
    David> http://search.cpan.org/~flora/Catalyst-TraitFor-Request-XMLHttpRequest-0.01/
    David> and then either render the error template like before OR use
    David> the status_not_found() helper from
    David> http://search.cpan.org/~bobtfish/Catalyst-Action-REST-0.87/

    David> To use status_not_found I need to extend my Root Controller
    David> from Catalyst::Controller::REST

    David> That'd mean i have to check the type of request again in my
    David> roots end method to avoid rendering of the template.


    David> #Root.pm
    David> sub render : ActionClass('RenderView') {}
    David> sub end :Private {
    David> my ($self, $c) = @_;
    David> unless ($c->request->is_xhr) {
    David> $c->forward('render');
    David> }
    David> }

    David> Any comments on that solution are appreciated as are pointers
    David> to alternatives.

    Put the ajax code into a separate controller with an XHR-specific end
    action (probably empty, for your case) and chain from there.

    --
    Eden Cardim Need help with your perl Catalyst or DBIx::Class project?
    Software Engineer http://www.shadowcat.co.uk/catalyst/
    Shadowcat Systems Ltd. Want a managed development or deployment platform?
    http://blog.edencardim.com http://www.shadowcat.co.uk/servers/
  • David Schmidt at Nov 9, 2010 at 3:06 pm

    On Tue, Nov 9, 2010 at 11:38 AM, Eden Cardim wrote:
    "David" == David Schmidt <davewood@gmx.at> writes:
    ? ?David> Hello list Both ajax and non-ajax actions chain off the same
    ? ?David> action (base_with_id). If an error occurs (like the resource
    ? ?David> doesn't exist) I detach to '/error404' in my Root Controller.
    ? ?David> I just introduced the ajax stuff and before that 'error404'
    ? ?David> simply rendered an error template. Now I'd like to check if
    ? ?David> it is an xmlhttp request using
    ? ?David> http://search.cpan.org/~flora/Catalyst-TraitFor-Request-XMLHttpRequest-0.01/
    ? ?David> and then either render the error template like before OR use
    ? ?David> the status_not_found() helper from
    ? ?David> http://search.cpan.org/~bobtfish/Catalyst-Action-REST-0.87/

    ? ?David> To use status_not_found I need to extend my Root Controller
    ? ?David> from Catalyst::Controller::REST

    ? ?David> That'd mean i have to check the type of request again in my
    ? ?David> roots end method to avoid rendering of the template.


    ? ?David> #Root.pm
    ? ?David> sub render : ActionClass('RenderView') {}
    ? ?David> sub end :Private {
    ? ?David> ? ? my ($self, $c) = @_;
    ? ?David> ? ? unless ($c->request->is_xhr) {
    ? ?David> ? ? ? ? $c->forward('render');
    ? ?David> ? ? }
    ? ?David> }

    ? ?David> Any comments on that solution are appreciated as are pointers
    ? ?David> to alternatives.

    Put the ajax code into a separate controller with an XHR-specific end
    action (probably empty, for your case) and chain from there.

    I tend to put related actions into a controller. E.g.: all actions
    dealing with artists into the Artists.pm controller and so on.
    You advise (as I understood) means having an additional controller for
    every controller where I want to support XHR requests.

    Is there a flaw in how I lay out my controllers?

    david
  • Eden Cardim at Nov 10, 2010 at 12:46 pm
    "David" == David Schmidt writes:
    David> I tend to put related actions into a controller. E.g.: all actions
    David> dealing with artists into the Artists.pm controller and so on.
    David> You advise (as I understood) means having an additional controller for
    David> every controller where I want to support XHR requests.

    David> Is there a flaw in how I lay out my controllers?

    Personally, I tend to treat controllers just like any other class, I factor them
    out when the implementation asks for it. For this case, I think breaking down
    into separate classes makes sense so you can leverage the catalyst action flow
    to model your application into layers that get more specific as you drill down
    the url hierarchy:

    /artist => calls MyApp::Controller::Root::end()
    /artist/1 => calls MyApp::Controller::Root::end()
    /artist/1/xhr => calls MyApp::Controller::Artist::XHR::end()

    also, since $c->controller (without any arguments) returns the controller of the
    current dispatched action, you can rely on it to determine the context of the
    generic actions, for instance, your error handling:

    $c->detach($c->controller->action_for('error_404'));

    in Controller::Artist:

    sub error_404 :Action {
    my($self, $c) = @_:
    $c->stash->{template} = 'error_404.tt'
    }

    in Controller::Artist::XHR:

    sub error_404 :Action {
    my($self, $c) = @_;
    $self->status_not_found($c,
    message => 'Cannot find what you were looking for!'
    );
    }

    --
    Eden Cardim Need help with your perl Catalyst or DBIx::Class project?
    Software Engineer http://www.shadowcat.co.uk/catalyst/
    Shadowcat Systems Ltd. Want a managed development or deployment platform?
    http://blog.edencardim.com http://www.shadowcat.co.uk/servers/
  • Octavian Rasnita at Nov 10, 2010 at 3:17 pm
    Hi,

    From: "Eden Cardim" <edencardim@gmail.com>
    in Controller::Artist:

    sub error_404 :Action {

    Can I find more information about the ":Action" dispatch type?

    Is it just a replacement for :Private? Or it is something different?

    Thanks.

    Octavian
  • Hernan Lopes at Nov 10, 2010 at 3:40 pm
    Octavian,

    Its just an action with attributes predefie ie:

    (in your controller):
    __PACKAGE__->config(
    action => {

    myaction =>
    { Chained => 'base', PathPart => q{myaction}, Args => 0, },
    });

    sub myaction :Action {
    my ( $self, $c,) => @_;

    ..............

    }

    --Hernan


    2010/11/10 Octavian Rasnita <orasnita@gmail.com>
    Hi,

    From: "Eden Cardim" <edencardim@gmail.com>
    in Controller::Artist:

    sub error_404 :Action {

    Can I find more information about the ":Action" dispatch type?

    Is it just a replacement for :Private? Or it is something different?

    Thanks.

    Octavian


    _______________________________________________
    List: Catalyst@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/20101110/484cfece/attachment.htm
  • Eden Cardim at Nov 10, 2010 at 6:16 pm
    "Octavian" == Octavian Rasnita writes:
    Octavian> Can I find more information about the ":Action" dispatch type?
    Octavian> Is it just a replacement for :Private? Or it is something different?

    Yes, it's effectively a replacement for :Private.

    --
    Eden Cardim Need help with your perl Catalyst or DBIx::Class project?
    Software Engineer http://www.shadowcat.co.uk/catalyst/
    Shadowcat Systems Ltd. Want a managed development or deployment platform?
    http://blog.edencardim.com http://www.shadowcat.co.uk/servers/
  • Ben van Staveren at Nov 9, 2010 at 10:39 am
    A bit offtopic but why use the trait at all, you can just check the
    x-requested-with header for the 'XMLHttpRequest' value - saves you a
    dependency. As far as the status_not_found routine, I suggest a liberal
    bit of cut 'n paste and using View::JSON (sort of negates the losing of
    one dependency if you give up the trait, unless you're using it already).

    David Schmidt wrote:
    Hello list

    Both ajax and non-ajax actions chain off the same action
    (base_with_id). If an error occurs (like the resource doesn't exist) I
    detach to '/error404' in my Root Controller.
    I just introduced the ajax stuff and before that 'error404' simply
    rendered an error template. Now I'd like to check if it is an xmlhttp
    request using http://search.cpan.org/~flora/Catalyst-TraitFor-Request-XMLHttpRequest-0.01/
    and then either render the error template like before OR use the
    status_not_found() helper from
    http://search.cpan.org/~bobtfish/Catalyst-Action-REST-0.87/
    --
    Ben van Staveren
    phone: +62 81 70777529
    email: benvanstaveren@gmail.com

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupcatalyst @
categoriescatalyst, perl
postedNov 9, '10 at 9:54a
activeNov 10, '10 at 6:16p
posts8
users5
websitecatalystframework.org
irc#catalyst

People

Translate

site design / logo © 2022 Grokbase