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