Permalink
Browse files

Remove before hook from Plugin::Ajax.

Unlike D1, there is only one way to defined an ajax route in D2, so we
CAN specify the *response* content type in the ajax route code instead
of a before hook. (cf Dancer1 commit 3dd7224)

Also fix a typo where the request content_type was being altered, remove
trailing whitespace, and apply doc patches and config options added to
Dancer::Plugin::Ajax since the initial fork.

Closes #141.
  • Loading branch information...
1 parent cf225e9 commit fbbeae579cd9fd9079b7a0a179504d88f6ea49c8 @veryrusty veryrusty committed with ambs Sep 1, 2013
Showing with 21 additions and 17 deletions.
  1. +21 −17 lib/Dancer2/Plugin/Ajax.pm
@@ -40,39 +40,45 @@ Disable the layout
=item *
-The action built is a POST request.
+The action built matches POST / GET requests.
=back
-=cut
+=head1 CONFIGURATION
+
+By default the plugin will use a content-type of 'text/xml' but this can be overrided
+with plugin setting 'content_type'.
+
+Here is example to use JSON:
+
+ plugins:
+ 'Ajax':
+ content_type: 'application/json'
+
-on_plugin_import {
- my $dsl = shift;
- $dsl->app->add_hook(
- Dancer2::Core::Hook->new(
- name => 'before',
- code => sub {
- if ( $dsl->request->is_ajax ) {
- $dsl->request->content_type('text/xml');
- }
- }
- )
- );
-};
+
+=cut
register 'ajax' => sub {
my ( $dsl, $pattern, @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 );
@@ -86,5 +92,3 @@ register 'ajax' => sub {
register_plugin;
1;
-
-

0 comments on commit fbbeae5

Please sign in to comment.