| 1 | package Catalyst::TraitFor::Request::XMLHttpRequest; | ||||||||||
| 2 | 1 | our $VERSION = '0.01'; | |||||||||
| 3 | # ABSTRACT: A request trait for XMLHttpRequest detection support | ||||||||||
| 4 | |||||||||||
| 5 | 1 | ▶ | use Moose::Role; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | 1 | ▶ | use MooseX::Types::Moose qw(Bool); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | 1 | ▶ | use namespace::autoclean; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 8 | |||||||||||
| 9 | |||||||||||
| 10 | 1 | has is_xhr => ( | |||||||||
| 11 | is => 'ro', | ||||||||||
| 12 | isa => Bool, | ||||||||||
| 13 | lazy => 1, | ||||||||||
| 14 | builder => '_build_is_xhr', | ||||||||||
| 15 | ); | ||||||||||
| 16 | |||||||||||
| 17 | 3 | ▶ | sub _build_is_xhr { | ||||||||
|
Subroutine
_build_is_xhr: called CC=3 SCAR=11.0
| |||||||||||
| 18 | 3 | my ($self) = @_; | |||||||||
| 19 | 3 | my $req_with = $self->header('X-Requested-With'); | |||||||||
| 20 | |||||||||||
| 21 | 3 | ▶ | return 0 unless defined $req_with; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 22 | 2 | ▶ | return 0 if $req_with ne 'XMLHttpRequest'; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 23 | 1 | return 1; | |||||||||
| 24 | } | ||||||||||
| 25 | |||||||||||
| 26 | 1 | 1; | |||||||||
| 27 | |||||||||||
| 28 | __END__ | ||||||||||
| 29 | =pod | ||||||||||
| 30 | |||||||||||
| 31 | =head1 NAME | ||||||||||
| 32 | |||||||||||
| 33 | Catalyst::TraitFor::Request::XMLHttpRequest - A request trait for XMLHttpRequest detection support | ||||||||||
| 34 | |||||||||||
| 35 | =head1 VERSION | ||||||||||
| 36 | |||||||||||
| 37 | version 0.01 | ||||||||||
| 38 | |||||||||||
| 39 | =head1 SYNOPSIS | ||||||||||
| 40 | |||||||||||
| 41 | Setting up the request trait for your application: | ||||||||||
| 42 | |||||||||||
| 43 | package MyApp; | ||||||||||
| 44 | |||||||||||
| 45 | use Moose; | ||||||||||
| 46 | use CatalystX::RoleApplicator; | ||||||||||
| 47 | use namespace::autoclean; | ||||||||||
| 48 | |||||||||||
| 49 | extends 'Catalyst'; | ||||||||||
| 50 | |||||||||||
| 51 | __PACKAGE__->apply_request_class_roles(qw( | ||||||||||
| 52 | Catalyst::TraitFor::Request::XMLHttpRequest | ||||||||||
| 53 | )); | ||||||||||
| 54 | |||||||||||
| 55 | __PACKAGE__->setup; | ||||||||||
| 56 | |||||||||||
| 57 | 1; | ||||||||||
| 58 | |||||||||||
| 59 | Using the trait in your controllers | ||||||||||
| 60 | |||||||||||
| 61 | sub some_action : Path('foo') { | ||||||||||
| 62 | my ($self, $ctx) = @_; | ||||||||||
| 63 | |||||||||||
| 64 | # do something depending on the request being an XMLHttpRequest or not | ||||||||||
| 65 | if ($ctx->request->is_xhr) { | ||||||||||
| 66 | ... | ||||||||||
| 67 | } | ||||||||||
| 68 | else { | ||||||||||
| 69 | ... | ||||||||||
| 70 | } | ||||||||||
| 71 | } | ||||||||||
| 72 | |||||||||||
| 73 | =head1 DESCRIPTION | ||||||||||
| 74 | |||||||||||
| 75 | This request trait adds support for detecting XMLHttpRequests to the Catalyst | ||||||||||
| 76 | request. | ||||||||||
| 77 | |||||||||||
| 78 | =head1 ATTRIBUTES | ||||||||||
| 79 | |||||||||||
| 80 | =head2 is_xhr | ||||||||||
| 81 | |||||||||||
| 82 | This attribute contains a boolean value indicating whether or not the request | ||||||||||
| 83 | is a XMLHttpRequest. | ||||||||||
| 84 | |||||||||||
| 85 | =head1 AUTHOR | ||||||||||
| 86 | |||||||||||
| 87 | Florian Ragwitz <rafl@debian.org> | ||||||||||
| 88 | |||||||||||
| 89 | =head1 COPYRIGHT AND LICENSE | ||||||||||
| 90 | |||||||||||
| 91 | This software is copyright (c) 2010 by Florian Ragwitz. | ||||||||||
| 92 | |||||||||||
| 93 | This is free software; you can redistribute it and/or modify it under | ||||||||||
| 94 | the same terms as the Perl 5 programming language system itself. | ||||||||||
| 95 | |||||||||||
| 96 | =cut | ||||||||||
| 97 | |||||||||||