NAME
Dancer::Plugin::ExtDirect - ExtDirect plugin for Dancer
VERSION
version 1.03
SYNOPSIS
use Dancer::Plugin::ExtDirect;
# basic example:
extdirect {
api => '/api',
actions => {
'Calculator' => {
sum => { len => 2, handler => \&sum },
},
},
};
sub sum {
my ($a, $b) = @_;
return $a + $b;
}
# a bit more complex example:
any qr{ /projects/.* }x => sub {
# chain route handlers to check permissions
# for ExtDirect calls too
pass;
};
extdirect {
api => '/projects/*/api', # the wildcard values are passed to handlers
namespace => 'MyApp',
actions => {
'Project' => {
addUser => { len => 1, handler => \&addUser },
},
},
};
sub addUser {
my ($project_id, $user) = @_;
...
}
# in HTML:
<script type="text/javascript" src="/api"></script>
<script type="text/javascript" src="/projects/2/api"></script>
<script type="text/javascript">
alert("3 + 2 = " + Calculator.sum(3,2));
MyApp.Project.addUser({ name => 'Harry' });
</script>
METHODS
extdirect
This method sets up a Dancer route handler to expose some functions to your JavaScript client-side application. It accepts a hashref containing the following options.
- api
-
This accepts a route handler URI path, such as
/api
. You can also use Dancer wildcards such as/projects/*/api
, so that the values caught are passed to your method handlers. - namespace
-
This option is injected to the addProvider method call.
- provider_config
-
This accepts a hashref with additional config options that will be injected to the addProvider method call.
- actions
-
This accepts a hashref whose keys are ExtDirect class names and their values are hashrefs with the method definitions (see the synopsis above for an example). Each method is defined by a hashref having the following keys:
- len
-
The number of arguments that the exported function accepts.
- handler
-
A coderef (or reference to a subroutine) that will handle the request. Note that this module doesn't force you to map the exposed ExtDirect class to any particular Perl class layout. The handler subroutine will be called with the arguments coming from the client-side. If you used any wildcards in the
api
path, the values caught (with Dancer'ssplat
method) will be prepended to the arguments. - formHandler
-
Optional. Mark this as true to handle ExtJs form (see ExtJs docs about the formHandler API).
- namespace
-
Optional. The JavaScript namespace under which the API will be exported.
- debug
-
Optional. Mark this as true to send ExtDirect exceptions when the handler dies. Default is false, meaning that Dancer will just throw a 500 Internal Server Error with no details exposed.
AUTHOR
Alessandro Ranellucci <aar@cpan.org>
COPYRIGHT AND LICENSE
This software is copyright (c) 2012 by Alessandro Ranellucci.
This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.