| 1 | package RPC::ExtDirect::Demo::TestAction; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | |||
| 6 | use Carp; | ||
| 7 | |||
| 8 | use RPC::ExtDirect Action => 'TestAction'; | ||
| 9 | |||
| 10 | sub doEcho : ExtDirect(1) { | ||
| 11 | my ($class, $data) = @_; | ||
| 12 | |||
| 13 | return $data; | ||
| 14 | } | ||
| 15 | |||
| 16 | sub multiply : ExtDirect(1) { | ||
| 17 | my ($class, $num) = @_; | ||
| 18 | |||
| 19 | croak "Call to multiply with a value that is not a number" | ||
| 20 | unless $num =~ / \A \d+ \z /xms; | ||
| 21 | |||
| 22 | return $num * 8; | ||
| 23 | } | ||
| 24 | |||
| 25 | sub getTree : ExtDirect(1) { | ||
| 26 | my ($class, $id) = @_; | ||
| 27 | |||
| 28 | return if length $id == 3; | ||
| 29 | |||
| 30 | # Construct root node elements | ||
| 31 | return [ map { { id => "n$_", text => "Node $_", leaf => \0 } } 1..5 ] | ||
| 32 | if $id eq 'root'; | ||
| 33 | |||
| 34 | my ($parent) = $id =~ /n(\d)/; | ||
| 35 | |||
| 36 | # Construct leaf node elements | ||
| 37 | return [ | ||
| 38 | map { { id => "$id$_", text => "Node $parent.$_", leaf => \1 } } 1..5 | ||
| 39 | ]; | ||
| 40 | } | ||
| 41 | |||
| 42 | sub getGrid : ExtDirect( params => [ 'sort' ] ) { | ||
| 43 | my ($class, %params) = @_; | ||
| 44 | |||
| 45 | my $field = $params{sort}->[0]->{property}; | ||
| 46 | my $direction = $params{sort}->[0]->{direction}; | ||
| 47 | |||
| 48 | my $sort_sub = sub { | ||
| 49 | my ($foo, $bar) = $direction eq 'ASC' ? ($a, $b) | ||
| 50 | : ($b, $a) | ||
| 51 | ; | ||
| 52 | return $field eq 'name' ? $foo->{name} cmp $bar->{name} | ||
| 53 | : $foo->{turnover} <=> $bar->{turnover} | ||
| 54 | ; | ||
| 55 | }; | ||
| 56 | |||
| 57 | my @data = sort $sort_sub ( | ||
| 58 | { name => 'ABC Accounting', turnover => 50000 }, | ||
| 59 | { name => 'Ezy Video Rental', turnover => 106300 }, | ||
| 60 | { name => 'Greens Fruit Grocery', turnover => 120000 }, | ||
| 61 | { name => 'Icecream Express', turnover => 73000 }, | ||
| 62 | { name => 'Ripped Gym', turnover => 88400 }, | ||
| 63 | { name => 'Smith Auto Mechanic', turnover => 222980 }, | ||
| 64 | ); | ||
| 65 | |||
| 66 | return [ @data ]; | ||
| 67 | } | ||
| 68 | |||
| 69 | sub showDetails : ExtDirect(params => [qw(firstName lastName age)]) { | ||
| 70 | my ($class, %params) = @_; | ||
| 71 | |||
| 72 | my $first = $params{firstName}; | ||
| 73 | my $last = $params{lastName}; | ||
| 74 | my $age = $params{age}; | ||
| 75 | |||
| 76 | return "Hi $first $last, you are $age years old."; | ||
| 77 | } | ||
| 78 | |||
| 79 | 1; | ||
| 80 | |||
| 81 | =pod | ||
| 82 | |||
| 83 | =head1 NAME | ||
| 84 | |||
| 85 | RPC::ExtDirect::Demo::TestAction - Part of Ext.Direct interface demo | ||
| 86 | |||
| 87 | =head1 DESCRIPTION | ||
| 88 | |||
| 89 | This module implements TestAction class used in ExtJS Ext.Direct demo | ||
| 90 | scripts; it is not intended to be used per se but rather as an example. | ||
| 91 | |||
| 92 | I decided to keep it in the installation tree so that it will always | ||
| 93 | be available to look up without going to CPAN. | ||
| 94 | |||
| 95 | =head1 SEE ALSO | ||
| 96 | |||
| 97 | You can use C<perldoc -m RPC::ExtDirect::Demo::TestAction> to see the actual | ||
| 98 | code. | ||
| 99 | |||
| 100 | =head1 AUTHOR | ||
| 101 | |||
| 102 | Alexander Tokarev E<lt>tokarev@cpan.orgE<gt> | ||
| 103 | |||
| 104 | =head1 LICENSE AND COPYRIGHT | ||
| 105 | |||
| 106 | Copyright (c) 2011-2016 by Alexander Tokarev. | ||
| 107 | |||
| 108 | This module is free software; you can redistribute it and/or modify it under | ||
| 109 | the same terms as Perl itself. See L<perlartistic>. | ||
| 110 | |||
| 111 | =cut | ||
| 112 |