CatalystX-ExtJS-Direct

 view release on metacpan or  search on metacpan

lib/CatalystX/ExtJS/Tutorial/Direct.pm  view on Meta::CPAN

    $c->res->body( $c->stash->{add} + $arg );
 }
    
 sub echo : Local : Direct : DirectArgs(1) {
    my ($self, $c) = @_;
    $c->res->content_type('application/json');
    $c->res->body(encode_json($c->req->data));
 }

As you can see the C<add_to> action has the C<Direct> attribute attached
to it. Direct actions can only be attached to endpoints of Chained actions.
By default the method's name for the API is the same as the action's
name. In this case however we changed the name of the action to C<add> by
adding this as parameter to the C<Direct> attribute.

If you add the Direct attribute to a normal action (e.g. C<Local>)
it has no arguments by default. To change that you can add the C<DirectArgs>
attribute and enter the number of arguments there. If you add C<DirectArgs>
to a Chained endpoint the number of arguments will be added to the number
of arguments required to call this endpoint.

The C<echo> action accepts one argument from the Direct API. You can access this argument
via C<< $c->req->data >>, which is always an arrayref and includes all arguments.
We set the content type to C<application/json> to make sure that the
body is not serialized twice. That is, if you would not set the content type,
the Direct router assumes that the body should be send "as is" to the client.
Usually you would use L<Catalyst::View::JSON> to do this for you.

Run the server (C<# script/myapp_server.pl -r>) and access
L<http://localhost:3000/api>.



( run in 0.284 second using v1.01-cache-2.11-cpan-b61123c0432 )