| Catalyst-TraitFor-Request-XMLHttpRequest documentation | Contained in the Catalyst-TraitFor-Request-XMLHttpRequest distribution. |
Catalyst::TraitFor::Request::XMLHttpRequest - A request trait for XMLHttpRequest detection support
version 0.01
Setting up the request trait for your application:
package MyApp;
use Moose;
use CatalystX::RoleApplicator;
use namespace::autoclean;
extends 'Catalyst';
__PACKAGE__->apply_request_class_roles(qw(
Catalyst::TraitFor::Request::XMLHttpRequest
));
__PACKAGE__->setup;
1;
Using the trait in your controllers
sub some_action : Path('foo') {
my ($self, $ctx) = @_;
# do something depending on the request being an XMLHttpRequest or not
if ($ctx->request->is_xhr) {
...
}
else {
...
}
}
This request trait adds support for detecting XMLHttpRequests to the Catalyst request.
This attribute contains a boolean value indicating whether or not the request is a XMLHttpRequest.
Florian Ragwitz <rafl@debian.org>
This software is copyright (c) 2010 by Florian Ragwitz.
This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.
| Catalyst-TraitFor-Request-XMLHttpRequest documentation | Contained in the Catalyst-TraitFor-Request-XMLHttpRequest distribution. |
package Catalyst::TraitFor::Request::XMLHttpRequest; our $VERSION = '0.01'; # ABSTRACT: A request trait for XMLHttpRequest detection support use Moose::Role; use MooseX::Types::Moose qw(Bool); use namespace::autoclean; has is_xhr => ( is => 'ro', isa => Bool, lazy => 1, builder => '_build_is_xhr', ); sub _build_is_xhr { my ($self) = @_; my $req_with = $self->header('X-Requested-With'); return 0 unless defined $req_with; return 0 if $req_with ne 'XMLHttpRequest'; return 1; } 1; __END__