# ABSTRACT: a plugin for adding Ajax route handlers package Dancer::Plugin::Ajax; { $Dancer::Plugin::Ajax::VERSION = '2.0000_01'; } use strict; use warnings; use Dancer ':syntax'; use Dancer::Plugin; hook 'before' => sub { if (request->is_ajax) { content_type('text/xml'); } }; register 'ajax' => sub { my ($dsl, $pattern, @rest) = @_; my $code; for my $e (@rest) { $code = $e if (ref($e) eq 'CODE') } my $ajax_route = sub { # must be an XMLHttpRequest if (not $dsl->request->is_ajax) { $dsl->pass and return 0; } # disable layout my $layout = $dsl->setting('layout'); $dsl->setting('layout' => undef); my $response = $code->(); $dsl->setting('layout' => $layout); return $response; }; $dsl->any(['get', 'post'] => $pattern, $ajax_route); }; register_plugin for_versions => [2]; 1; __END__ =pod =head1 NAME Dancer::Plugin::Ajax - a plugin for adding Ajax route handlers =head1 VERSION version 2.0000_01 =head1 SYNOPSIS package MyWebApp; use Dancer; use Dancer::Plugin::Ajax; ajax '/check_for_update' => sub { # ... some Ajax code }; dance; =head1 DESCRIPTION The C 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 doesnt equal XMLHttpRequest =item * Disable the layout =item * The action built is a POST request. =back =head1 AUTHOR Dancer Core Developers =head1 COPYRIGHT AND LICENSE This software is copyright (c) 2012 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