| 1 | package Dancer2::Plugin::Ajax; | ||||||||||
| 2 | # ABSTRACT: a plugin for adding Ajax route handlers | ||||||||||
| 3 | 2 | $Dancer2::Plugin::Ajax::VERSION = '0.400000'; | |||||||||
| 4 | 2 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | 2 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | 2 | ▶ | use Dancer2::Core::Types 'Str'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | 2 | ▶ | use Dancer2::Plugin; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 8 | |||||||||||
| 9 | has content_type => ( | ||||||||||
| 10 | is => 'ro', | ||||||||||
| 11 | isa => Str, | ||||||||||
| 12 | 0 | ▶ | from_config => sub { 'text/xml' }, | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||
| 13 | 2 | ); | |||||||||
| 14 | |||||||||||
| 15 | 2 | plugin_keywords 'ajax'; | |||||||||
| 16 | |||||||||||
| 17 | 4 | ▶ | sub ajax { | ||||||||
|
Subroutine
ajax: called CC=5 SCAR=16.1
Pod
undocumented
| |||||||||||
| 18 | 4 | my ( $plugin, $pattern, @rest ) = @_; | |||||||||
| 19 | |||||||||||
| 20 | 4 | my @default_methods = ( 'get', 'post' ); | |||||||||
| 21 | |||||||||||
| 22 | # If the given pattern is an ArrayRef, we override the default methods | ||||||||||
| 23 | 4 | ▶ | if( ref($pattern) eq "ARRAY" ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 24 | 1 | @default_methods = @$pattern; | |||||||||
| 25 | 1 | $pattern = shift(@rest); | |||||||||
| 26 | } | ||||||||||
| 27 | |||||||||||
| 28 | 4 | my $code; | |||||||||
| 29 | 4 | ▶ | for my $e (@rest) { $code = $e if ( ref($e) eq 'CODE' ) } | ||||||||
|
Branch: 1 branch
| |||||||||||
| 30 | |||||||||||
| 31 | 8 | ▶ | my $ajax_route = sub { | ||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.0
| |||||||||||
| 32 | |||||||||||
| 33 | # # must be an XMLHttpRequest | ||||||||||
| 34 | 8 | ▶ | if ( not $plugin->app->request->is_ajax ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 35 | 2 | $plugin->app->pass; | |||||||||
| 36 | } | ||||||||||
| 37 | |||||||||||
| 38 | # Default response content type is either what's defined in the | ||||||||||
| 39 | # plugin setting or text/xml | ||||||||||
| 40 | 6 | ▶ | $plugin->app->response->header('Content-Type') | ||||||||
|
Branch: 1 branch
| |||||||||||
| 41 | or $plugin->app->response->content_type( $plugin->content_type ); | ||||||||||
| 42 | |||||||||||
| 43 | # disable layout | ||||||||||
| 44 | 6 | my $layout = $plugin->app->config->{'layout'}; | |||||||||
| 45 | 6 | $plugin->app->config->{'layout'} = undef; | |||||||||
| 46 | 6 | my $response = $code->(@_); | |||||||||
| 47 | 6 | $plugin->app->config->{'layout'} = $layout; | |||||||||
| 48 | 6 | return $response; | |||||||||
| 49 | 4 | }; | |||||||||
| 50 | |||||||||||
| 51 | 4 | foreach my $method ( @default_methods ) { | |||||||||
| 52 | 9 | $plugin->app->add_route( | |||||||||
| 53 | method => $method, | ||||||||||
| 54 | regexp => $pattern, | ||||||||||
| 55 | code => $ajax_route, | ||||||||||
| 56 | ); | ||||||||||
| 57 | } | ||||||||||
| 58 | } | ||||||||||
| 59 | |||||||||||
| 60 | 2 | 1; | |||||||||
| 61 | |||||||||||
| 62 | __END__ | ||||||||||