The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

NAME

Web::Util::ExtPaging - Paginate DBIx::Class::ResultSets for ExtJS consumption

VERSION

version 0.001003

SYNOPSIS

  package MyApp::People;

  use Web::Simple;
  use JSON::MaybeXS;
  use Web::Util::ExtPaging;

  sub dispatch_request {
    my $people_rs = get_rs();

    sub (/people) {
      [
         200,
         [ 'Content-type', 'application/json' ],
         [ encode_json(ext_paginate($rs->search(undef, { rows => 25 }))) ],
      ]
    },
    sub (/people_lite) {
      [
         200,
         [ 'Content-type', 'application/json' ],
         [
            encode_json(ext_paginate(
               $rs->search(undef, { rows => 25 }), sub {
                  my $person = shift;
                  return {
                     first_name => $person->first_name,
                     last_name => $person->last_name,
                  }
               },
            ))
         ],
      ]
    },
    sub (/people_more_different) {
      [
         200,
         [ 'Content-type', 'application/json' ],
         [
            # this will call the 'foo' method on each person and put the
            # returned value into the datastructure
            encode_json(ext_paginate(
               $rs->search(undef, { rows => 25 }), 'foo',
            ))
         ],
      ]
    },
    sub (/programmers_do_it_by_hand) {
      [
         200,
         [ 'Content-type', 'application/json' ],
         [ encode_json(ext_parcel([qw( foo bar baz )], 10)) ],
      ]
    },
    sub (/programmers_do_it_by_hand_partially) {
      [
         200,
         [ 'Content-type', 'application/json' ],
         # defaults total to amount of items passed in
         [ encode_json(ext_parcel([qw( foo bar baz )])) ],
      ]
    },
    sub () { [ 404, [ 'Content-type', 'text/plain' ], [ 'not found' ] ] }
  }

DESCRIPTION

This module is mostly for sending DBIx::Class paginated data to ExtJS based javascript code.

METHODS

ext_paginate

  my $json      = ext_paginate($resultset, { root => 'root' });
  my $json_str  = json_encode($json);

Description

Returns a structure like the following from the ResultSet:

  {
     data  => \@results,
     total => $count_before_pagination
  }

Valid arguments are:

  rs - paginated ResultSet to get the data from
  (optional) coderef - any valid scalar that can be called on the result object
  (optional) config - passed to ext_parcel

ext_parcel

  my $items    = [qw{foo bar baz}];
  my $total    = 7;
  my $json     = $self->ext_parcel($data, $total, { root => 'root' });
  my $json_str = to_json($json);

Description

Returns a structure like the following:

  {
     data  => [@{$items}],
     total => $total || scalar @{$items}
  }

Valid arguments are:

  list  - a list of anything you want to be in the data structure
  total - whatever you want to say the total is.  Defaults to size of
          the list passed in.
  (optional) config - a hashref containing root or total_property.  root is the
          key used to store the data under, total_property is the key used to
          store the total under

SEE ALSO

Catalyst::TraitFor::Controller::DoesExtPaging, which this module was factored out of.

AUTHOR

Arthur Axel "fREW" Schmidt <frioux+cpan@gmail.com>

COPYRIGHT AND LICENSE

This software is copyright (c) 2015 by Arthur Axel "fREW" Schmidt.

This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.