/usr/share/perl5/Dancer2/Plugin/Ajax.pm is in libdancer2-perl 0.152000+dfsg-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 | # ABSTRACT: a plugin for adding Ajax route handlers
package Dancer2::Plugin::Ajax;
$Dancer2::Plugin::Ajax::VERSION = '0.152000';
use strict;
use warnings;
use Dancer2;
use Dancer2::Plugin;
register 'ajax' => sub {
my ( $dsl, $pattern, @rest ) = @_;
my $default_methods = [ 'get', 'post' ];
# If the given pattern is an ArrayRef, we override the defaults
# and pass these onto to DSL->any()
if( ref($pattern) eq "ARRAY" ) {
$default_methods = $pattern;
$pattern = shift(@rest);
}
my $code;
for my $e (@rest) { $code = $e if ( ref($e) eq 'CODE' ) }
my $content_type = plugin_setting->{content_type} || 'text/xml';
my $ajax_route = sub {
# must be an XMLHttpRequest
if ( not $dsl->request->is_ajax ) {
$dsl->pass and return 0;
}
# Default response content type is either what's defined in the
# plugin setting or text/xml
$dsl->response->header('Content-Type')
or $dsl->response->content_type( $content_type );
# disable layout
my $layout = $dsl->setting('layout');
$dsl->setting( 'layout' => undef );
my $response = $code->();
$dsl->setting( 'layout' => $layout );
return $response;
};
$dsl->any( $default_methods => $pattern, $ajax_route );
};
register_plugin;
1;
__END__
=pod
=encoding UTF-8
=head1 NAME
Dancer2::Plugin::Ajax - a plugin for adding Ajax route handlers
=head1 VERSION
version 0.152000
=head1 SYNOPSIS
package MyWebApp;
use Dancer2;
use Dancer2::Plugin::Ajax;
# For GET / POST
ajax '/check_for_update' => sub {
# ... some Ajax code
};
# For all valid HTTP methods
ajax ['get', 'post', ... ] => '/check_for_more' => sub {
# ... some Ajax code
};
dance;
=head1 DESCRIPTION
The C<ajax> keyword which is exported by this plugin allow you to define a route
handler optimized for Ajax queries.
The route handler code will be compiled to behave like the following:
=over 4
=item *
Pass if the request header X-Requested-With doesn't equal XMLHttpRequest
=item *
Disable the layout
=item *
The action built matches POST / GET requests by default. This can be
extended by passing it an ArrayRef of allowed HTTP methods.
=back
=head1 CONFIGURATION
By default the plugin will use a content-type of 'text/xml' but this can be
overridden with plugin setting 'content_type'.
Here is example to use JSON:
plugins:
Ajax:
content_type: 'application/json'
=head1 AUTHOR
Dancer Core Developers
=head1 COPYRIGHT AND LICENSE
This software is copyright (c) 2014 by Alexis Sukrieh.
This is free software; you can redistribute it and/or modify it under
the same terms as the Perl 5 programming language system itself.
=cut
|